Jump to content

odd behavior writing code to popup


ginerjm

Recommended Posts

I'm opening up a popup window from my php code using a js function.  Then I try to write some info and a short js function to the popup.  Been playing with this for about 3 hours.  IE gives me an error message "unterminated string constant" when I include the following lines within the rest of my js "write" statements:

 

myWindow.document.write( "<script type='text/javascript'>" );
myWindow.document.write( "function setFocus()	{" );
myWindow.document.write( "document.getElementById('btn').focus();}" );
myWindow.document.write( "</script>" );

 

Without this code my js function is error free and works.  With it IE says that line xxx has an unterminated string.  Line xxx happens to be (every time!) the last line with the "</script>" piece.  Take it out and I don't get an error but my end product doesn't work since the lack of the closing script tag ruins the whole function's usefulness.

 

What is wrong with that simple write statement?

Link to comment
Share on other sites

You need to use:

 

myWindow.document.write( "<" + "/script>" );

 

There's actually a recent thread about this in the misc board as well. Because JS is wrapped in HTML tags, the browser first looks for the opening and closing script tags, then the JS engine parses what's in the middle. So the browser finds the closing script tag, even though it's within a string, and passes the JS engine a broken script.

Link to comment
Share on other sites

Now that the ending Script tag works, I'm struggling to get the function to work properly.  The layout is - my main window is opening up a popup that will show some info to the user.  I define my popup, open it, populate it with the data, include a button to close the window, and the last piece is to set the focus on the button.  There's the problem and here's the entire code:

function openAddrWin(nm,idx)
{
	var url ='';
	var width = 230;
	var height = 190;
	var left = parseInt((screen.availWidth/2) - (width/2));
	var top = parseInt((screen.availHeight/2) - (height/2));
	var addr_parts = Addresses[idx].split("|");
	var windowFeatures = "width=" + width + ",height=" + height + ",left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
	windowFeatures += ",status=yes,location=no,menubar=yes,directories=no,resizable=yes,toolbar=no,titlebar=no,scrollbars=no";
	var myWindow = window.open(url, "subWind", windowFeatures);
	myWindow.document.write( "<html><head>" );
	myWindow.document.write( "<style type='text/css'>" );
	myWindow.document.write( "#div1 {float:left;position:relative;margin-left:20px;}" );
	myWindow.document.write( "</style>" );
	myWindow.document.write( "<script type='text/javascript'>" );
	myWindow.document.write( "function setFocus()" );
	myWindow.document.write( "{alert('in setFocus');document.getElementById('btn').focus();return;}" );
	myWindow.document.write( "</" + "script>" );
	myWindow.document.write( "</head>" );
	myWindow.document.write( "<body bgcolor='#c0c0c0'>" );
	myWindow.document.write( "<div id='div1'>" );
	myWindow.document.write( "<center>Mailing Address for: </center><br>" );
	myWindow.document.write( nm + "<br>" );
	myWindow.document.write( addr_parts[0] + "<br>" );
	if (addr_parts[1] != '')
		myWindow.document.write( addr_parts[1] + "<br>" );
	myWindow.document.write(addr_parts[2]+", " + addr_parts[3] + " " + addr_parts[4] + "<br>" );
	myWindow.document.write("<form name='thisform'>");
	myWindow.document.write( "<center><input type='button' name='btn' id='btn' value='Close'  onclick='window.close()'></center>" );
	myWindow.document.write( "</form>" );
	myWindow.document.write( "</div></body></html>" );
	setTimeout(myWindow.setFocus(),2000);
}

The very last line (settimeout) is supposed to wait 2 seconds and then execute a function that will place the focus on my 'Close' button in the new popup.  I'm using setTimeout because I figure an onload won't work since there is no button at the time the popup's load finishes, so I thought that by waiting a bit and then calling a function to set the focus would be the trick.  The problem is that the settimout doesn't wait for 2 seconds - it executes immediately.  I've tried setInterval as well - no luck.

 

Any ideas?

Link to comment
Share on other sites

Including the () after the function means that you're invoking it immediately, and then passing into the setTimeout function anything it may return. Just drop those and you will be passing the function itself:

 

setTimeout(myWindow.setFocus, 2000);

Link to comment
Share on other sites

Ya know - I read something about () just this morning, but didn't make the connection to my own circumstances.  Doh!

 

So - now my timeout works.  But - the setFocus doesn't.  Here's that specific code:

myWindow.document.writeln( "<script type='text/javascript'>" );
myWindow.document.writeln( "function setFocus()" );
myWindow.document.writeln( "{document.getElementById('btn').focus();" );
myWindow.document.writeln( "alert('should have setFocus');" );
myWindow.document.writeln( "return;}" );
myWindow.document.writeln( "</" + "script>" );

 

The Alert happens, but the actually setting of focus on my button doesn't happen.

 

And just so you can see it here is the button definition:

myWindow.document.writeln("<form name='thisform' id='thisform'>");
myWindow.document.writeln( "<center><input type='button' name='btn' id='btn' value='Close'  onclick='window.close()' tabindex=1></center>" );
myWindow.document.writeln( "</form>" );

 

This is a lot tougher than I thought it would be.  :)

 

 

Link to comment
Share on other sites

Hmm.. Is there a reason you're writing all of the pop-up's contents using JS? Why not place it all in an HTML file you specify in open(), and then just use JS to add in the extra bits of data you need? Or perhaps even easier, pass the data in as parameters and build the HTML using PHP?

Link to comment
Share on other sites

This started out as such a simple task I thought the JS way would be easier.  Now that it's turned out to be more of a bear, I kinda wonder what I'm doing wrong.  But you're right - maybe it's time to punt if you don't see the problem.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.