Riccardo Pedrielli
Riccardo Pedrielli

Reputation: 237

Reload a page inside iframe

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?

Upvotes: 1

Views: 1183

Answers (2)

hereandnow78
hereandnow78

Reputation: 14434

just not possible, see this thread:

Get current URL from IFRAME

and this one

How do I get the current location of an iframe?

Upvotes: 1

Michael Bearjaws
Michael Bearjaws

Reputation: 201

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

Upvotes: 0

Related Questions