Using relative url for window.location in child iframe

I am having problems with an iframe related issue.

I have a document (parent) with an iframe (child). If the parent document calls a function in the child that changes the childs URL using window.location to a relative URL, that URL becomes relative to the original document. Let me illustrate.

Parent document: /test/parent.html

Children's document: /projects/child.html

Code for children:

function moveOn(){
    window.location.href = "newPage.html";
}

Parent code:

childDocument.contentWindow.moveOn();

Result: A child document tries to access /test/newPage.html instead of /projects/newPage.html

? , , .

, Nik

Edit:

parent (/testr/testr.html):

testr.appHolder = $("childIframe"); 
testr.appHolder.src = "../projects/test.html"; 
testr.functionName = "moveOn"; 
testr.appHolder.contentWindow[testr.functionName]();

child (/projects/test.html):

function moveOn(){ 
  window.location.href = "newPage.html"; 
}

parent (/testr/testr.html):

testr.appHolder = $("childIframe"); 
testr.appHolder.src = "../projects/test.html"; 
testr.functionName = "moveOn"; 
testr.appHolder.contentWindow[testr.functionName]();

child (/projects/test.html):

function moveOn(){ 
  window.location.href = "newPage.html"; 
}
+3
3

, !

childDocument.contentWindow.setTimeout( childDocument.contentWindow.moveOn, 0 );

(moveOn) , Internet Exploder.

Booyah.

+4

HTML :

[location ], [] URL- script, , URL-.

() - pathname location : URL- .

URL- "", url.

Dotdot

"" - , "", "dotdot", URL-.

childDocument.contentWindow.location.pathname += "/../newPage.html";

( , , .)

, .


/projects/child.html

,
/projects/child.html/../newPage.html


/projects/newPage.html

, , . , "" , . "" URL- .

, , url , . , .

Sidenote: , . .

-1

. baseURI. .

, baseURI , (), . , baseURI , , , , URL .

, :

var originalURI = document.baseURI; // store for setting back after the move
document.baseURI = childDocument.location; // setting parent baseURI (NO EFFECT!)
childDocument.contentWindow.moveOn();
document.baseURI = originalURI; // restore original baseURI

, baseURI . , <base>, script. , childUrl:

<head>
<base href="/path/to/child.html" />
</head>

, , , . , moveOn().

<base /> <head>, baseURI . , moveOn() , - :

var baseElement = document.getElementsByTagName("base")[0];

// adopt the child location as baseURI
baseElement.href = childDocument.location; 

childDocument.contentWindow.moveOn();

// revert back to the parent original baseURI
baseElement.removeAttribute("href"); 

(, - - ... )

Edit:

Internet Exploder . , . , IE (HA!), , . , ifs.

-1
source

Source: https://habr.com/ru/post/1733184/


All Articles