Jump to content

Javascript with AJAX and maybe an anchor tag to control URL?


treeleaf20

Recommended Posts

All,

I have some code on my page which changes images in a div without reloading the page. This works great, however my problem comes with I go through a couple images and the actual URL is still on the original image. Is there any way to change the URL when I click the image as well??

 

An example is facebook:

http://www.facebook.com/photo.php?pi...015&id=9316995

 

When I click on an image, it goes to:

http://www.facebook.com/photo.php?pi...017&id=9316995

 

Then when you click refresh in your browser, it goes to this:

http://www.facebook.com/photo.php?pi...017&id=9316995

 

So if I have this code:

echo "<div style=\"position:relative;\"><img src=\"uploaded_files/$resultsetstory[image_id]\" onclick=\"javascript:getpic(this.myform2);\"></div>";  

Would I need to put an anchor tag link around this?

Link to comment
Share on other sites

So if I have the following code:

var http_request = false;
   function makePOSTRequest(url, parameters, str) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }

      http_request.onreadystatechange = alertContents;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }

   function alertContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            document.getElementById('myspan').innerHTML = result;
  			resizeAll();
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
   
   function getpic(obj) {
  var poststr = "picture=" + encodeURI( document.getElementById("picture").value );
      makePOSTRequest('getnextpic.php', poststr);
   }

 

Would I put the location.hash in here?? How would this work?

Link to comment
Share on other sites

The 'responseText' returned from the AJAX request I assume contains the HTML image tag? What would an example look like? You'd need to modify the URL within your "alertContents()" function, after you've set the "result" var.

 

I'm also assuming you want to append the ID of the image after the hash? In which case it may be worth re-thinking what the AJAX request returns. With the data you have now, if the image ID is even contained, you'd have to parse it out. If you return say an array (image_id, image_src, etc.) makes it easier to work with. You could then generate the HTML from that data and also modify the URL with the image ID. At the moment you're mixing the logic and presentation in a bad way.

Link to comment
Share on other sites

So I did some research and found this little bit of code:

function realUrl() {

    if (window.location.hash.match(/\.php/)) {

        return 'http://'+window.location.host+window.location.hash.split('#')[1];

    } else if (window.location.href.indexOf('#') != -1) {

        return window.location.hash.split('#')[0];

    } else {

      return window.location.href;

    }

}

 

However, I'm not sure how to make this work with the code I currently have. Any suggestions?

 

Thanks for the continued help.

Link to comment
Share on other sites

Actually,

I'd use:

	function redirectHash()
{
	var hash = location.hash;
	if (hash){
		hash = hash.replace('#', '');
		location.href = hash;
	}
}

    </script> 

 

But I'd have to parse out the picture_id. If it's contained in an <img> tag and will always have an id of "showpictureid" how can I find this id?

 

Thanks.

Link to comment
Share on other sites

I tried this:

var imgs = document.getElementsByTagName("img");
for ( var d = 0; d < imgs.length; ++d )
    {				
     var img = imgs[d];
     if ( img.className == "showpictureid" )
        {
      imgid = imgs[d].getAttribute("id");
      //alert("The image id is" + imgid);
}
}

 

But it doesn't work. The PHP code is:

echo "<div style=\"position:relative;\"><img src=\"uploaded_files/$resultsetstory[image_id]\" class=\"showpictureid\" id=\"$resultsetstory[image_id]\" onclick=\"javascript:getpic(this.myform2);\"></div>";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.