0

I am working on ajax and using .load() function to load the files/pages on click. Its working perfectly. I want to show the file/page location as well. Here is my html :

<ul class="nav nav-tabs nav-stacked">
    <li role="presentation" ><a id="link" >
         <span class="glyphicon glyphicon-     phone"></span> Responsive Pages</a>   
    </li>
</ul>
<div id="loadfilename"></div>
<div id="loadpage"></div>

and I am using following javascript

$("#link").click(function(){
    $("#loadpage").load("responsivepages.html");
    $("#loadfilename").html(this.location.href);
});

Its not showing any thing. but if i use $("#loadfilename").html(window.location.href);

its shows my current url which is my home page. But I want to get location of clicked file/page.

Here is jsfiddle link https://jsfiddle.net/a52f50sy/

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
  • Your 'this' context in your example is the anchor element. Perhaps you want the anchor's owner document? – t1nr2y Sep 16 '15 at 20:09
  • How can i use anchor tag in there? I tried before but ajax request was not working. –  Sep 16 '15 at 20:30
  • 1
    I'm sorry, I'm not quite sure what your asking. What I mean is that the anchor tag is the element you have bound your click event to, and so when you type "this.location.href", you are attempting to access the location.href from the anchor element. Maybe use Javascript/JQuery to find the ownerDocument, which should have the href. For example, $(this)[0].ownerDocument.URL. – t1nr2y Sep 16 '15 at 20:43

1 Answers1

0

The URL of a resource you load using Ajax isn't stored anywhere unless you store it somewhere. Nor does loading a resource via Ajax doesn't change the URL of the current page.

So store that URL somewhere:

var url = "responsivepages.html";
$("#loadpage").load(url);
$("#loadfilename").html(url);
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • But its not showing complete path. Is there any way i could get path? –  Sep 16 '15 at 20:07
  • http://stackoverflow.com/questions/14780350/convert-relative-path-to-absolute-using-javascript – Quentin Sep 16 '15 at 20:10