...

View Full Version : How do you include an outside file into script?



MrBiggZ
06-16-2010, 01:00 AM
Greetings!

I have this script and I want to take out the part below the /////////////////////////// up to the buildMenu(); statement. I would dynamically create the menu list with php. Problem is I don't know javascript that well :(

This is its own .js file not a script within the head section of the page.



<!--

/*
Configure menu styles below
NOTE: To edit the link colors, go to the STYLE tags and edit the ssm2Items colors
*/
YOffset=150; // no quotes!!
XOffset=0;
staticYOffset=30; // no quotes!!
slideSpeed=20 // no quotes!!
waitTime=100; // no quotes!! this sets the time the menu stays out for after the mouse goes off it.
menuBGColor="black";
menuIsStatic="yes"; //this sets whether menu should stay static on the screen
menuWidth=150; // Must be a multiple of 10! no quotes!!
menuCols=2;
hdrFontFamily="verdana";
hdrFontSize="2";
hdrFontColor="white";
hdrBGColor="#170088";
hdrAlign="left";
hdrVAlign="center";
hdrHeight="15";
linkFontFamily="Verdana";
linkFontSize="2";
linkBGColor="white";
linkOverBGColor="#FFFF99";
linkTarget="_top";
linkAlign="Left";
barBGColor="#444444";
barFontFamily="Verdana";
barFontSize="2";
barFontColor="white";
barVAlign="center";
barWidth=20; // no quotes!!
barText="SIDE MENU"; // <IMG> tag supported. Put exact html for an image to show.

///////////////////////////

// ssmItems[...]=[name, link, target, colspan, endrow?] - leave 'link' and 'target' blank to make a header
ssmItems[0]=["Menu"] //create header
ssmItems[1]=["Dynamic Drive", "http://www.dynamicdrive.com", ""]
ssmItems[2]=["What's New", "http://www.dynamicdrive.com/new.htm",""]
ssmItems[3]=["What's Hot", "http://www.dynamicdrive.com/hot.htm", ""]
ssmItems[4]=["Message Forum", "http://www.codingforums.com", "_new"]
ssmItems[5]=["Submit Script", "http://www.dynamicdrive.com/submitscript.htm", ""]
ssmItems[6]=["Link to Us", "http://www.dynamicdrive.com/link.htm", ""]

ssmItems[7]=["FAQ", "http://www.dynamicdrive.com/faqs.htm", "", 1, "no"] //create two column row
ssmItems[8]=["Email", "http://www.dynamicdrive.com/contact.htm", "",1]

ssmItems[9]=["External Links", "", ""] //create header
ssmItems[10]=["JavaScript Kit", "http://www.javascriptkit.com", ""]
ssmItems[11]=["Freewarejava", "http://www.freewarejava.com", ""]
ssmItems[12]=["Coding Forums", "http://www.codingforums.com", ""]

buildMenu();

//-->


I don't know the syntax to do something like this:



<!--

/*
Configure menu styles below
NOTE: To edit the link colors, go to the STYLE tags and edit the ssm2Items colors
*/
YOffset=150; // no quotes!!
XOffset=0;
staticYOffset=30; // no quotes!!
slideSpeed=20 // no quotes!!
waitTime=100; // no quotes!! this sets the time the menu stays out for after the mouse goes off it.
menuBGColor="black";
menuIsStatic="yes"; //this sets whether menu should stay static on the screen
menuWidth=150; // Must be a multiple of 10! no quotes!!
menuCols=2;
hdrFontFamily="verdana";
hdrFontSize="2";
hdrFontColor="white";
hdrBGColor="#170088";
hdrAlign="left";
hdrVAlign="center";
hdrHeight="15";
linkFontFamily="Verdana";
linkFontSize="2";
linkBGColor="white";
linkOverBGColor="#FFFF99";
linkTarget="_top";
linkAlign="Left";
barBGColor="#444444";
barFontFamily="Verdana";
barFontSize="2";
barFontColor="white";
barVAlign="center";
barWidth=20; // no quotes!!
barText="SIDE MENU"; // <IMG> tag supported. Put exact html for an image to show.

///////////////////////////

// ssmItems[...]=[name, link, target, colspan, endrow?] - leave 'link' and 'target' blank to make a header

include menu-file

buildMenu();

//-->

Not really sure how to go about this :confused:
Thanks for any help in advance!

Old Pedant
06-16-2010, 01:16 AM
You can't have a ".js" file include another ".js" file (or equivalent), but you don't need to.

Just have your *MAIN* page build the array *BEFORE* you include the ".js" file! (After removing the lines you indicated, of course.)

So your main (presumably PHP) page would do:


<html>
<head>
<script type="text/javascript">
var ssmItems = [
<?php
// ... php loop that generates the menu items ... such as ...
$delim = ""; // delimiter is blank on first line
while ( whatever )
{
echo $delim . "[\"$name\",\"$url\",\"\"]\n";
$delim = ","; // change delimiter to a comma
...
}
...
?>
]; // end of the js array
</script>
<script type="text/javascript" src="thatFileYouShowed.js"></script>
...

The idea is to have your PHP page, when you look at the source code it produces in the browser, end up with HTML of the form

[code]
<html>
<head>
<script type="text/javascript">
var ssmItems = [
["Home","/home.php",""]
,["Pictures","/pix.php",""]
,["About Me","/about.php",""]
]; // end of the js array
</script>


Okay?

MrBiggZ
06-16-2010, 05:21 PM
Thanks Old Pedant!!!

I didn't realize you could do that! Huh, so you can declare the array in javascript and use it in another script in the head section.

That's the third thing I learned today! 1st thing was that I still can dodge rain drops ... even when I'm turned sideways!

Much thanks again! :thumbsup:

Old Pedant
06-16-2010, 08:00 PM
You need to understand that there is *NO DIFFERENCE* in how the code is executed, regardless of whether you put it inline in your HTML page:


<script type="text/javascript">
var foo = 37;
function bar( )
{
alert(foo);
}
</script>

or you put it into an external JS file:


<script type="text/javascript" src="zamboni.js"></script>

Essentially, when you put it into the external JS file, that just tells the browser to go grab the *source code* from that external file and copy/paste it in place into the web page.

So there's no difference between initializing a variable inline or initializing it in the external file.

NOW...

*IF* the variable is DECLARED *inside a function*, then the variable is *only* "visible" inside that same function. But that would be true whether the function was coded inline or in the external file.

That is, of course, *not* the case here: In fact, that code was *designed* to be used with a variable that is declared at page scope (that is, outside of any function) and, I think, the author should have made that clear by either using two separate external files or by showing and example coded as I did, with the the variable in the main page.

Dean440
06-16-2010, 09:31 PM
You can't have a ".js" file include another ".js" file (or equivalent), but you don't need to.

Just have your *MAIN* page build the array *BEFORE* you include the ".js" file! (After removing the lines you indicated, of course.)

So your main (presumably PHP) page would do:


<html>
<head>
<script type="text/javascript">
var ssmItems = [
<?php
// ... php loop that generates the menu items ... such as ...
$delim = ""; // delimiter is blank on first line
while ( whatever )
{
echo $delim . "[\"$name\",\"$url\",\"\"]\n";
$delim = ","; // change delimiter to a comma
...
}
...
?>
]; // end of the js array
</script>
<script type="text/javascript" src="thatFileYouShowed.js"></script>
...


Okay?

Using PHP to write JavaScript? I can't believe I never thought of that before. Wonder if I'll ever find a use for that.:cool:

Old Pedant
06-16-2010, 10:12 PM
I can't imagine writing really useful web pages *without* doing so.

(Don't mean every page, of course, but at least some pages in any set of pages would need to do so.)

Just some examples:
-- creating dependent <select>s. example: countries, states, cities. Use PHP/ASP/JSP to create JS that changes which states appear for a given country and the which cities for a given state, where you are reading a database of that info.

-- creating slideshows of images, where you get the list of available images from either finding all files in a directory or (again) from a database.

-- creating click-on locations on a google map, based on latitude/longitude table in (what else?) a database.

And many many more.

Dean440
06-17-2010, 12:10 AM
Isn't that more of an AJAX thing using XMLHttpRequest that you're talking about there? I was specifically talking about actually writing the portions of non-AJAX applications within the <script> tags using PHP, or was that what you were talking about?

Old Pedant
06-17-2010, 05:28 AM
??? I can't see any reason to use AJAX for any of those.

Oh, you *MIGHT* want to use AJAX for the first one, *IF* the number of <option> values possible for that last <select> is quite large (e.g., if you had 10 countries and 400 states, you'd certainly do that without AJAX, but you *might* opt to use AJAX for the cities if you had 8,000 cities). But I wouldn't bother with the mess of AJAX if I had no more than, say, 2000 cities all total.

And I certainly woud never use AJAX in the other two situations. Makes no sense. Why add overhead for no reason?

hdewantara
06-18-2010, 02:13 PM
Hi,

You can't have a ".js" file include another ".js" file (or equivalent), but you don't need to...

A bit far from topic if I may,
wonder whether we could do that via document.createElement("script") ?

Say we have lib1.js as follows:

var
libs = document.getElementsByTagName("script");
lib2 = document.createElement("script");
lib2.type="text/javascript";
lib2.src="lib2.js";

// but which line below is more appropriate to insert the new lib
// as part of document?
libs[0].parentNode.insertBefore(lib2,libs[libs.length-1]);
//libs[0].parentNode.appendChild(lib2);
//document.getElementsByTagName("head")[0].appendChild(lib2);
...

Old Pedant
06-18-2010, 08:30 PM
Yeah, there's always a way around a limitation. But certainly overkill for the current instance, no?

hdewantara
06-18-2010, 09:29 PM
Possibly, or ...
I don't know, to be precise :o.

I have never applied that idea (yet) to real applications,
but just on my own PC.

Maybe that's why popular jquery's fancybox always requires
at least 2 script include files: the core jquery and fancybox pack itself,
not just fancybox.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum