Forum Moderators: open

Message Too Old, No Replies

Master JS File - Creating a per page call function?

I have the idea and the format of execution...

         

JAB Creations

3:43 am on Nov 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This script...
addEvent(window, 'load', function() {
document.getElementById('query_string').focus()
});

...is part of a master JS file and specifically intended for content pages. I have music player pages which are also linked to the master JS file.

Both scripts need to somehow only be loaded on their intended pages. I have an idea of how to execute this but I need a little help refining it.

content.html will probally have something like...

<script type="text/javascript">
$pagetype = 'content';
</script>

Then for the script only intended for the content page would be something like...

if ($pagetype = 'content'){ *content script executes*}
else {return false;}

I figure it would be something like that (and vice versa for the other type of page. I haven't tried it yet but I am interested in input from the folks here. :)

kaled

11:15 am on Nov 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If there is a unique characteristic of the target pages (e.g. a <div id="content">) you could detect that rather than adding an extra bit of js to each target page.

Kaled.

Bernard Marx

11:18 am on Nov 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'll second that.

JAB Creations

8:35 pm on Nov 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh good idea!

Then how would I use getElementsByTagName to say...

If getElementsByTagName "content" exists execute function "contentfunc"?

JAB Creations

12:32 am on Nov 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Playing with this in class...

<script type="text/javascript">
if (document.getelementsbytagname("content")!= "undefined") { alert ("undefined") }
else {alert ("Defined")}
</script>

<div id="content">
<p>Hello World</p>
</div>

Bernard Marx

12:43 am on Nov 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if (document.getElementById("content"))
alert ("undefined");
else
alert ("Defined");

You will also have to wait until the page has loaded.

Alternatively, you could put the code you want to run into a separate script file, and only include it in the files you want.

JAB Creations

12:59 am on Nov 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is stuck as always alerting as defined even though there is no id that = "content"?

<html>
<head>
<title>Test</title>
</head>

<body>

<script type="text/javascript">
if (document.getElementById("content")) {alert ("undefined");}
else {alert ("Defined");}
</script>

<div id="bob">
<p>Hello World</p>
</div>

</body>
</html>

Bernard Marx

1:16 am on Nov 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, slight mistake:

if (! document.getElementById("content"))...

Bernard Marx

1:19 am on Nov 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



With your current structure, the element will always be undefined (even if there is one on the page), because the code is executed before the HTML for it has been parsed.

JAB Creations

5:48 am on Nov 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Working...limited though.
<html>
<head>
<title>Test</title>
</head>
<body>

<div id="content">
<p>Hello World</p>
</div>

<script type="text/javascript">
if (! document.getElementById("content")) {alert ("undefined");}
else {alert ("Defined");}
</script>

</body>
</html>

Yeah...how will I be able to make this work in my master JS file? I've encountered this problem with script having to be in the page AFTER the code it was interacting with and I never figured that out. :(