Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to use document.write to create a function

Status
Not open for further replies.

fedtrain

Instructor
Jun 23, 2004
142
US
Hello,
I have been struggling all morning so far...I have to create links that open a new window and load a .swf. I am using document.write to create the popup and load all the code for the .swf.

That works fine. But then I try to write a function and the whole thing dies.

Code:
function openSWF(){
var toolWindow=window.open('','name','toolbar=no,location=no,status=no,menubar=no,
   scrollbars=no,resizable=no,width=500,height=350');
toolWindow.document.write('<!DOCTYPE html PUBLIC "-
    //W3C//DTD XHTML 1.0 Transitional//EN" 
    "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-[/URL]
    transitional.dtd">')
toolWindow.document.write('<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL]
     xml:lang="en" lang="en"> ');
toolWindow.document.write('<head><title>ToolsBox</title>');
toolWindow.document.write("<script src='../../../Scripts/AC_RunActiveContent.js' 
    type='text/javascript'></script>");
toolWindow.document.write('</head>');
toolWindow.document.write('<body>');
toolWindow.document.write("<script language='JavaScript' type='text/javascript'>")
toolWindow.document.write('<!-- ')
toolWindow.document.write (" function setVariable() 
    {thisMovie(pasVar.swf).SetVariable ('text.Var.text', 
    Hello);}")
toolWindow.document.write (' -->')
toolWindow.document.write ('</script>')
toolWindow.document.write("<p>Here's the page, did it work?</p>");
toolWindow.document.write('<div align="center">')
toolWindow.document.write("<script type='text/javascript'> ")
toolWindow.document.write("AC_FL_RunContent('codebase','[URL unfurl="true"]http://download.macromedia.com/pub/shockwave/[/URL]
    /flash/swflash.cab#version=7,0,19,0','width','400','height',
    '250','title','testmovie','src','passVar','quality','high',
    'pluginspage','[URL unfurl="true"]http://www.macromedia.com/go/getflashplayer',[/URL]
    'swLiveConnect','true','movie','passVar' );")
toolWindow.document.write(' //end AC code ')
toolWindow.document.write('</script> <noscript>')
toolWindow.document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ')
toolWindow.document.write ('codebase="[URL unfurl="true"]http://download.macromedia.com/pub/shockwave/[/URL]
    cabs/flash/swflash.cab#version=7,0,19,0" width="400" height="250" 
    title="testmovie">')
toolWindow.document.write('<param name="movie" value="passVar.swf" />')
toolWindow.document.write('<param name="quality" value="high" />')
toolWindow.document.write('<embed src="passVar.swf" quality="high" pluginspage="[URL unfurl="true"]http://www.macromedia.com/go/getflashplayer"[/URL] 
    type="application/x-shockwave-flash" 
    width="400" height="250"')
toolWindow.document.write ('swLiveConnect = "true">')
toolWindow.document.write ('</embed>')
toolWindow.document.write('</object></noscript><hr />')
toolWindow.document.write('</div>')	
toolWindow.document.write('<div align="right"><a href="javascript:self.close()">Close</a> the popup.</div>')
toolWindow.document.write ('</body></html>');

toolWindow.document.close();
}

So it is this part that kills everything...
Code:
toolWindow.document.write('<!-- ')
toolWindow.document.write (" function setVariable() {thisMovie(pasVar.swf).SetVariable ('text.Var.text', Hello);}")
toolWindow.document.write (' -->')

If I cut those then it works. The problem is that I need to pass some variables over to the .swf based on the link clicked and so I need the function to be on the page to trigger.


"Credit belongs to the man who is actually in the arena - T.Roosevelt
 
You really don't need the HTML comment delimiters for something like that.

Lee
 
EH?

"Credit belongs to the man who is actually in the arena - T.Roosevelt
 
These:
<!--
-->

are HTML comment delimiters. They are typically used these days to get the search engines to skip reading the Javascript and just deal with the HTML. Since everything is dynamically created, they serve no purpose.

Also, I always split opening and closing script tags so the browser doesn't mistake them for opening or closing script tags in the script that's running:

'</scr' + 'ipt>'

Lee
 
Just writing the code to the body won't run it. Can't tell you why. I made this script to dynamically load a javascript into the document head. I use it for AJAX injecting scripts. And it works.

Code:
function load_script(filename) {
  var script = document.createElement('script'); 
  script.type = 'text/javascript'; 
  script.src = filename; 
  document.getElementsByTagName('head')[0].appendChild(script);
  if(debug)
    monitor(dynScripts, filename);
}
 
Writing HTML and Javascript to a new page is different from AJAX.

Lee
 
Ok....so I will try to pull out the "<---" things and see what happens.

I will also try to split up all the 'script' tags.

Another question if I may...why do some of the examples on the web have code like this...."http:\/\/something.com\/somethingelse\/" It didn't seem to matter to what I was doing and it was more complicated to keep track of.

"Credit belongs to the man who is actually in the arena - T.Roosevelt
 
The backslash is called an "escape" character. It indicates that Javascript is to process the character after it differently than it normally would. Escaping forward slashes isn't usually necessary in character strings, though they're necessary in regular expressions. The main use I have with backslashes is with quotation marks inside a string so JS doesn't consider the quote the end of the string. If you want JS to process a single backslash, you need to use two of them (\\) so the interpreter understands you weren't just escaping the next character.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top