Click to See Complete Forum and Search --> : JS within JS
FlyByProgrammer
08-08-2003, 12:30 PM
I'm new to javascript and have been trying to figure out some things. Currenty I'm trying to use javascript like SSI. Normally I wouldn't do this, but my server won't support SSI. I have my standard html document and the only code in it is a reference to a javascript page (layout.js). layout.js contains the layout for the standard table I use on all my html pages. This way I only have to type the layout once and it keeps the code consolidated, etc. That worked fine.
Next I tried to expand upon the layout javascript code. In layout.js I embedded links to other javascript pages that will contain the navigation and page content. This was a problem. If I did the usual reference "<script type="text/javascript" src="nav.js"></script>" then the html page could no longer display layout.js. If I put document.write(" ") around the reference to nav.js, the navigation page would be displayed, but outside the table layout.js had established. Can someone help with a way to embed javascript within javascript? The code I was using will be posted below.
FlyByProgrammer
08-08-2003, 12:36 PM
index.html
<html>
<head>
<title>Test</title>
</head>
<body>
<script type="text/javascript" src="layout.js"></script>
</body>
</html>
layout.js
<!--
document.write("<table border='3' width='920' height'='2000' cellpadding='10'>")
document.write("<tr height='70'>")
document.write("<td></td>")
document.write("<td align='center'>")
document.write("TOP")
document.write("</td>")
document.write("<td></td>")
document.write("</tr>")
document.write("<tr height='1900'>")
document.write("<td valign='top' width='150'>")
document.write("LEFT")
document.write("</td>")
document.write("<td valign='top' width='550'>")
document.write("MIDDLE")
document.write("</td>")
document.write("<td width='220' valign='top'>")
document.write("RIGHT")
document.write("</td>")
document.write("</tr>")
document.write("<tr>")
document.write("<td colspan='3' align='center'>")
document.write("BOTTOM")
document.write("</td>")
document.write("</tr>")
document.write("</table>")
//-->
The areas saying "TOP," "LEFT," "RIGHT," and "BOTTOM" are where I'm trying to put links to other javascript pages. This current code is working if you want to paste it into an editor.
AdamGundry
08-08-2003, 02:02 PM
You shouldn't be making pages which depend on the user having Javascript - 13% of web users have it disabled. If you can't generate the code server-side, you should hard code it.
As far as I'm aware, the only way you can do what you want (in some browsers) is:
document.write('<script type="text/javascript" src="somefile.js"></script>');
Adam
David Harrison
08-08-2003, 02:03 PM
You can replace TOP, for example, with this:
document.write("<script type="text/javascript" src="top.js"></sc"+"ript>");
It doesn't like the </script> closing tag, so you just split it up.
FlyByProgrammer
08-08-2003, 02:15 PM
It's still not working right. When I split the end script like this </sc"+"ript>, it didn't read the code any further. If somebody has time could they load the following code? The HTML opens and loads the layout.js. From the layout.js it also loads then nav.js. But the nav.js doesn't appear in the left side of the table. Is there a solution?
index.html
<html>
<head>
<title>Test</title>
</head>
<body>
<script type="text/javascript" src="layout.js"></script>
</body>
</html>
layout.js (with link to nav.js)
<!--
document.write("<table border='3' width='920' height'='2000' cellpadding='10'>")
document.write("<tr height='70'>")
document.write("<td></td>")
document.write("<td align='center'>")
document.write("TOP")
document.write("</td>")
document.write("<td></td>")
document.write("</tr>")
document.write("<tr height='1900'>")
document.write("<td valign='top' width='150'>")
document.write("<script type='text/javascript' src='nav.js'></script>");
document.write("</td>")
document.write("<td valign='top' width='550'>")
document.write("MIDDLE")
document.write("</td>")
document.write("<td width='220' valign='top'>")
document.write("RIGHT")
document.write("</td>")
document.write("</tr>")
document.write("<tr>")
document.write("<td colspan='3' align='center'>")
document.write("BOTTOM")
document.write("</td>")
document.write("</tr>")
document.write("</table>")
//-->
nav.js
document.write("the nav bar loaded")
thanks for your help
David Harrison
08-08-2003, 02:33 PM
You said that you split the closing </script> tag, but then you posted this:
document.write("<script type='text/javascript' src='nav.js'></script>");
which should be this:
document.write("<script type='text/javascript' src='nav.js'></sc"+"ript>");
FlyByProgrammer
08-08-2003, 02:42 PM
I reset it becaues the split version terminated the code at the </sc"+"ript>. The rest of the table wasn't displayed.
David Harrison
08-08-2003, 02:53 PM
I just realised something, I gave you the solution to something else. This should work for what you want:
document.createElement("script").src="nav.js";
FlyByProgrammer
08-08-2003, 03:47 PM
I put that line in my code. The good thing was the whole table was displayed and the html file opened without any errors. The problem still is that the nav.js file didn't load. It just leaves a blank table cell. What am I doing wrong?
I replaced the LEFT section with this code:
document.write("<td valign='top' width='150'>")
document.createElement("script").src="nav.js";
document.write("</td>")
David Harrison
08-08-2003, 03:51 PM
Hmmm, try this then:
new_thing=document.createElement("script");
new_thing.src="nav.js";
FlyByProgrammer
08-08-2003, 04:55 PM
I don't get it. It's not working again. Did you try the code and it worked for you? That last piece of code does the same thing as the previos one. Do I have to declare any variables or do other things like that?
David Harrison
08-08-2003, 05:35 PM
I'll get it working by tomorrow, I used to have something similar on a site once.
FlyByProgrammer
08-09-2003, 08:56 AM
I tried creating variables, it didn't help. Still not sure what to do.
David Harrison
08-09-2003, 03:18 PM
All credit to AdamBrill as I completely missed off the last line of the script in 1.js. I had to consult a past thread before I realised this.
FlyByProgrammer
08-09-2003, 03:39 PM
Thanks, I'll see what I can work out with this.
David Harrison
08-09-2003, 03:52 PM
I'm sure that Adam's happy to help. :)