Why is it so that when you use window.parent.showMessage("Video Is OK"); inside a .js file you've included on a page, it won't work, but only if it's on the page itself..?
Can you fix this?
There are two scenarios that I can think of where you'd want to use window.parent. The first is when you have a window open another window using window.open. The other is where the first window uses an iframe to load a page. In the former case, it appears as though you actually want to use window.opener, as ukostin has said. In the latter case, window.parent works fine. Both methods work properly whether the code is inline or loaded from an external JS file. Here are some tests:
POPUP
parentWindow.htm:
<html>
<head>
<script>function showMsg(msg){alert(msg);}</script>
<body>
Open
</body>
</html>
externalWindow.js:
function showMsgExternal(msg){window.opener.showMsg(msg);}
childWindow.htm:
<html>
<head>
<script>function showMsgInline(msg){window.opener.showMsg(msg);}</script>
<script src="externalWindow.js"></script>
</head>
<body>
Inline
External
</body>
</html>
IFRAME
parentFrame.htm:
<html>
<head>
<script>function showMsg(msg){alert(msg);}</script>
</head>
<body>
<iframe src="childFrame.htm" width="300" height="100"></iframe>
</body>
</html>
externalFrame.js:
function showMsgExternal(msg){window.parent.showMsg(msg);}
childFrame.htm:
<html>
<head>
<script>function showMsgInline(msg){window.parent.showMsg(msg);}</script>
<script src="externalFrame.js"></script>
</head>
<body>
Inline
External
</body>
</html>
Try to use window.opener as link to the parent window.
Related
Essentially, all I want to do is open an external web page after the current page is loaded via java script.
open my page -> javascript tells browser to open external page -> external page being loaded into the broser
How may I accomplish this?
you may use this
<html>
<head>
<script type="text/javascript">
function load()
{
window.location.href = "http://externalpage.com";
}
</script>
</head>
<body onload="load()">
<h1>Hello World!</h1>
</body>
</html>
<html>
<head>
<script type="text/javascript">
function load()
{
window.location.href = "http://externalpage.com";
}
</script>
</head>
<body onload="load()">
<h1>Hello World!</h1>
</body>
</html>
Hope it should be window.location. Check the code.
Technically you can:
location.href = "http://example.net/";
… but you should perform an HTTP redirect instead as that is more reliable, faster and better food for search engines.
You can also use the "open" method to open the source file or url on a new window.
window.open("anyfile.*");
or
window.open("http://anylocation.com");
Hi please try this code for page loading time we will redirect whatever u configured the urls.
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
window.open('https://google.com');
}
</script>
</head>
<body onload="myFunction()">
</body>
</html>
<body>
<script>
document.body.innerHTML += 'Link';
document.getElementById("link").click();
</script>
<body>
SO, I know how to create a popup window in google, and I know how to write in the popup window, but how do I put in other HTML tags in a popup window?
The Html code that I have is below.
<html>
<head>
<script>
function openWin()
{
myWindow=window.open("","","width=200,height=100");
myWindow.document.write("<p>NEW WINDOW<p>");
}
</script>
</head>
<body>
<button onmousedown='openWin();'>Hello</button>
</body>
</html>
As you can see, in the document.write function, I have two <p> tags, and I can also use the <button> tag, but I cannot use the <script> tag, does anyone know why, or if it is possible?
So you're trying to write a <script> tag to your popup window? You have to do it like this:
<html>
<head>
<script>
function openWin()
{
myWindow=window.open("","","width=200,height=100");
myWindow.document.open();
myWindow.document.write("<p>NEW WINDOW<p>");
myWindow.document.write("<scr" + "ipt>alert('Script works!')</scr"+"ipt>");
myWindow.document.close();
}
</script>
</head>
<body>
<button onmousedown='openWin();'>Hello</button>
</body>
</html>
Otherwise the parent window will think the script tags are something it should interpret.
You cannot write "<script>" or "</script>" to the window, these are specifically disallowed. You have to split up these terms.
function openWin() {
myWindow=window.open("","","width=200,height=100");
myWindow.document.write("<p>NEW WINDOW<p>");
myWindow.document.write("<scr");
myWindow.document.write("ipt>alert('Script works!')</scr")
myWindow.document.write("ipt>");
}
Here is a snippet of code that uses a script to populate the contents of an iframe:
<!doctype html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
$(document).ready(function() {
$('iframe').contents().find('body').html('<script>console.log($("div"))<\/script>');
});
</script>
</head>
<body>
<div>Test</div>
<iframe />
</body>
</html>
When executed we see that the iframe has access to the parent's DOM and we see the div being selected by the jQuery selector. The iframe does not have jQuery included but it can access the jQuery object of the parent.
However if we write the same thing via an iframe src inclusion, the behavior is different:
test.html:
<!doctype html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
</head>
<body>
<div>Test</div>
<iframe src="another.html">
</body>
</html>
another.html:
<!doctype html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
$(document).ready(function() {
console.log($('div'));
});
</script>
</head>
<body>
</body>
</html>
We now see that the page does not list any divs. Further, if we don't include the jQuery js in the child page, it would throw an error.
Note that both pages are in the same domain, so we don't have same-origin policy issues.
My questions are:
Why is the behavior different for the 2 - a. manipulating the iframe DOM from the parent and b. including the iframe content via a src?
Is there a way to make the parent have access to the child and NOT vice-versa?
So the first bit of code gives 1 and the second bit of code gives 0?
That seems correct.
In the first example $ is bound to the parent frame. In the second example, since you have a new instance of jQuery it's bound to the iframe.
In:
$(document).ready(function() {
$('iframe').contents().find('body').html('<script>console.log($("div"))<\/script>');
});
jQuery's html function will do an eval on the script-part of the inserted HTML. That eval will run in the scope of the parent so it uses the parent instance of $.
If you just moved the script to the iframe it will fail because it doesn't have access to $.
In Javascript, I want to open my window.html file in a popup window. But it doesn't display any text. Just a blank page.
This is index.html:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<script language="javascript">
var newwindow;
function popit(url){
newwindow = window.open(
url, '', "status=yes, height=500; width=500; resizeable=0");
}
</script>
</head>
<body>
CLICK ME!
</body>
</html>
window.html:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>SAMPLE TEXT</p>
</body>
</html>
Why doesn't it display any text?
javascript:popit(window.html);
Replace with:
javascript:popit('window.html');
Your click handler code is syntactically incorrect:
CLICK ME!
Always, always have your developer console open to check for JavaScript errors! (edit — actually in this case there wouldn't have been an error; window.html would resolve to undefined probably! Still, keep the console open :-)
Also note that I used an "onclick" attribute instead of "href".
A GOOD working code with NO crashes.
Simple and what makes this code better is that you can use it in a JavaScript file separately and have it fairing to more then one file with the same popup size even though its different pages on popups.
Javascript
// Popup window code
function MyPopUp(url) {
popupWindow = window.open(
url,'popUpWindow','height=454,width=580,left=0,top=200,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes')
}
HTML
My PopUp
NOTE: You can also use this as onload in body for example <body onload="JavaScript:MyPopUp('MyDirectory/Page.html');"> and it will aslo work on onmouseover and others... though I do not advise this unless you want to piss off the clients visiting your page.
I want to be able to open a popup using window.open and subscribe to page events (onload etc) of the popup in the opener. So i'd want a method in my opener (parent page) to execute when the popup's onload or ready fires. Is this possible using plain js or jquery? Pls don't ask me why i want to do this - this can solve a lot of issues for me.
First page (x.html):
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
var w = window.open('y.html', 'w');
w.document.getElementById('target').onclick = function () { alert('!'); };
</script>
</body>
</html>
Second page (y.html):
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<button id="target">target</button>
</body>
</html>
Works for me...