window.open javascript problems jquery
update: will use jquery if needed... OK, I have almost everything working that I need...i just have a quick question. How can I put additional html/javascript into document.write function? I am experimenting with window.open (and yes, i know I should be calling up pages, but I want to do this all just using one page-- index.html) and I want to embed some images and another button but it won't work? What do I need to do to get this to work? (and where should the javascript for the button on win开发者_运维问答dow 2 go?)
Backstory: There is index.html with 1 link and 1 button. Button opens window 1, link opens window 2. Window 2 has a button that can close window 1. the end!
index.html:
<head>
<script type="text/javascript">
function openWin1()
{
myWindow=window.open('','','');
myWindow.document.write("<p>I want flower.jpg here</p>");
myWindow.focus();
}
function openWin2()
{
myWindow=window.open('','','');
myWindow.document.write("<p>I want bee.jpg here, a background color and a button
that will close window1</p>");
myWindow.focus();
}
</script>
</head>
<body>
<input type="button" value="Window 1" onclick="openWin1()" /><br />
<a href="javascript:openWin2()">Window 2</a>
</body>
</html>
function openWin1()
{
myWindow=window.open('','','');
myWindow.document.write("<p><img src=\"http://www.digital-photography-school.com/wp-content/uploads/2007/11/flower.jpg\" /></p>");
myWindow.focus();
}
function openWin2()
{
myWindow=window.open('','','');
myWindow.document.write("<style type=\"text/css\">body{background-color:yellow;}</style><p><img src=\"http://4.bp.blogspot.com/_YGkSujzLG1g/S8eFOjiQ7yI/AAAAAAAAAA8/wSRi2dGDr-c/s400/bee.jpg\" /></p><input type=\"button\" onclick=\"window.close();\" value=\"close me\" />");
myWindow.focus();
}
Working example: http://jsfiddle.net/AlienWebguy/rLmCR/
After reading your comments, I think your question really comes down to needing to know how to escape parentheses in a string. The reason you are getting syntax errors if you do something like this:
myWindow.document.write("<p>I want <img src="myimageurl">flower.jpg here</p>");
is that the "
is ending your string. You can use the \
character to escape the string, like this:
myWindow.document.write("<p>I want <img src=\"myimageurl\">flower.jpg here</p>");
which will not give you syntax errors.
Try this
<head>
<script type="text/javascript">
var Window1, Window2;
function openWin1()
{
Window1=window.open('','','');
Window1.document.write("<p>I want flower.jpg here</p>");
Window1.focus();
}
function openWin2()
{
Window2=window.open('','','');
Window2.document.write("<p>I want bee.jpg here, a background color and a button
that will close window1</p>");
Window2.focus();
}
</script>
</head>
<body>
<input type="button" value="Window 1" onclick="openWin1()" /><br />
<a href="javascript:openWin2()">Window 2</a>
</body>
</html>
To close Window1 from a button in Window2 use the below code
function closeWindow2(){
window.opener.Window1.close();
}
This button will be in Window2
<input type="button" onclick="closeWindow2()" />
精彩评论