0

I need help with getting URL of iFrame element in mozilla. While this code works perfectly in IE it does not work in mozilla.

<iframe id="frame" onload="myFunction()" src="./upload/" ></iframe>

<script type="text/javascript" >
   function myFunction()
   {                            
      var a = document.getElementById("frame").contentWindow.location.href;
      alert(a)
   }
</script>

Can you please tell me how to make it work in mozilla ? I don`t need the src of the iframe but changing URL according to iframe location. Webpage and iframe content are both on the same domain.

I found out that these two codes works perfectly only in IE:

document.getElementById("frame").contentWindow.location.href;
document.frames['frame'].location.pathname;

Also location.pathname and location.href without iframe work also in chrome and mozilla. But if i add document.getElementById("frame") or document.frames['frame'] it stops working in chrome and mozilla.

  • 1
    I think this is a duplicate of http://stackoverflow.com/questions/938180/get-current-url-from-iframe – Philip Mar 20 '17 at 13:51
  • Yes 7 years old post with same answer as i used in this question as not working code for mozilla – Ján Šutor Mar 21 '17 at 07:09
  • @JánŠutor - you should first comment to the chosen answer there asking for the person to update his answer. creating duplicates is a last thing one should do – vsync Mar 21 '17 at 08:03

1 Answers1

1

Your code, I tested this in FireFox 52.0.1 and IE 11.

<iframe id="frame" onload="myFunction()" src="http://stackoverflow.com/questions/42905168/iframe-url-in-mozilla/42905409" ></iframe>
<script type="text/javascript">
window.addEventListener("load", function(){
    document.getElementById('frame').onload = myFunction;
});
function myFunction() {
    var a = document.getElementById("frame").src;
    var parser = document.createElement('a');
    parser.href = a;
    parser.protocol; // => "http:"
    parser.hostname; // => "stackoverflow.com"
    parser.port;     // => "80"
    parser.pathname; // => "/questions/42905168/iframe-url-in-mozilla/42905409"
    parser.search;   // => ""
    parser.hash;     // => ""
    parser.host;     // => "stackoverflow:80"
    alert(a);
} 
</script>

There was a funny issue with IE not figuring out the onload event through Javascript.

To Get the src

window.addEventListener("load", function(){ alert(document.getElementById("frameid").src); });

The window.addEventListener is added to make sure the DOM is loaded completely.

To set the src document.getElementById('frameid').setAttribute("src",someurlhere);

Webbanditten
  • 850
  • 9
  • 21
  • This code will not help me as i need to get path of URL not set it. – Ján Šutor Mar 21 '17 at 07:07
  • Your sollution still tries to get src. I dont have problem with getting src but src is not dynamic it is static value ./folder/ I would like to get URL when i go to for example ./folder/subfolder/ – Ján Šutor Mar 21 '17 at 08:03
  • So you want to write out the folder content? I updated my answer and tested in Firefox And IE it works now. You can now get the src - The URL attribute. Note SO is not a coding service. – Webbanditten Mar 21 '17 at 08:05
  • No i dont need folder content only folder path. – Ján Šutor Mar 21 '17 at 08:07
  • It is working but it only show src of that frame : example.com/folder/ but when i go to subfolder in that iframe, it still shows example.com/folder/ not example.com/folder/subfolder/ Thats why i replaced src with location.pathname and it wont show anything. – Ján Šutor Mar 21 '17 at 08:15
  • Oh... Interesting. Do you can any JS Error? Because http://stackoverflow.com/questions/18195542/detecting-src-location-change-in-a-iframe-object should work as long as its within the same Origin. – Webbanditten Mar 21 '17 at 08:29
  • You are still working with src and src will not change when i go to subfolders of storage on server. I need to work with current URL – Ján Šutor Mar 21 '17 at 08:30
  • Is it now possible to put that path inside variable ? I dont need it to show alert window, that was only for test purpose. Now i need to put thaw path to variable so i could you is in another script. I cant figure out how to set in to variable inside HTML iframe code. – Ján Šutor Mar 21 '17 at 08:38