1

I have an iframe and I want to reload the currently displayed page on button press.

HTML:

<iframe id="webView"></iframe>

JS:

function reloadPage()
{
    var webView = document.getElementById("webView");

    //CODE
}

Inside the reloadPage() method I tried different solutions:

Call reload()

webView.contentWindow.location.reload();

This just doesn't work because the pages loaded inside the iframe are from a different domain than the main page.

Set src

webView.src = wevView.src;

It gives wrong result because it contains the initial url that I set to the iframe, non the current one.

Set location

webView.contentWindow.location = webView.contentWindow.location

I was expecting it to not work with urls from different domains (the same as calling reload()), but actually it works and also gives a good result.
Good but not perfect: the location object holds the current url but strips any parameter.

For example if the frame is currently displaying the following url:

http://www.myserver.com/thatsite/?page_id=11

the location object contains this url:

http://www.myserver.com/thatsite/

So this one works well as long as there are no parameters in the url.

Better solution?

I rely heavly on urls with parameters (mostly WordPress installations) so i need a way to keep them while reloading.
Anyone knows a solution to achieve this?

  • have you tried to set the iframe's src to nothing, wait a short time, and reset it again? – Alex Jul 31 '14 at 19:26
  • That is not the problem, the src=src method works without the need to wait. I need a way to get the current url, complete with all the parameters. – Riccardo Pedrielli Jul 31 '14 at 19:28

2 Answers2

1

just not possible, see this thread:

Get current URL from IFRAME

and this one

How do I get the current location of an iframe?

Community
  • 1
  • 1
hereandnow78
  • 14,094
  • 8
  • 42
  • 48
  • Is it normal that i can access and even set contentWindow.location? I was expecting not to be able to access anything under contentWindow, and this is stated two times in your first link. * If you try to access documentWindow.location.href **(or anything under documentWindow)** and the iframe is in a page that doesn't belong to the domain of the containing window, it will raise an exception * By the way **you can't access cross-domain iframe location** at all. I'm a bit confused about it. I'm experienced in C++, C# and Java but i'm new to js and html so i don't know where some boundaries are – Riccardo Pedrielli Jul 31 '14 at 20:07
  • not that firm with iframes, especially cross origin, as i try not to use them (and you should do the same), sorry... – hereandnow78 Jul 31 '14 at 21:28
0

Since setting location works, you could use location.search to retrieve the GET parameters and reconstruct the URL that way.

Example:

webView.contentWindow.location = webView.contentWindow.location + webView.contentWindow.location.search
  • will not work because of cross origin policy (his iframe url is from a different domain) – hereandnow78 Jul 31 '14 at 19:37
  • `webView.contentWindow.location.search` gives `Error: Permission denied to access property 'search'`, the same error i get when i try to call `reload()`. And i really don't understand why i can read and even set `location`, but i can't read anything inside it. – Riccardo Pedrielli Jul 31 '14 at 19:38