conditional javascript script

S

Steve

Hi All...

Got a question regarding javascript.

I am trying to use javascript to call a navigational bar based on screen
resolution:

<script language="Javascript" type="text/javascript"
if (screen.width <= 600) {
src="side_nav/side_nav.js"
}
else {
src="vert_nav1/vert_nav1.js"
}>
</script>

This is not working...soooo...anyone got any ideas as to why? I sure would
appreciate some help here.

TIA

Steve G
 
P

Peter Taurins

You'll probably find that the variable only exists inside your script.
So, you'll need to document.write the naviagtion inside the script rather
than set the variable src and try it use it outside the script.
 
S

Steve

Thanks for the responses, guys, unfortunately, I can't seem to get this
stuff to do what I want. Maybe a bit more explanation will help.

I am trying to set up this site so that it will pull different elements into
a page based on screen resolution. For instance, I have two style sheets,
one based on a screen resolution of 800 X 600, another one based on higher
resolutions, to set text sizes and some other things. This is working well.

Now I need to pull in navigational stuff again based on resolution. Nomally
I would just use an include file to do this such as:
<!-- Include file="something.htm" --> but in this instance I need to base it
on screen resolution. And rather than an html file, I was trying to pull in
the script instead. Since I can't seem to get that to work, I tried this
instead:

<script language="Javascript" type="text/javascript">
if (screen.width <= 600) {
document.write('<!-- #Include file=side_nav/side_nav.html -->');
}
else {
document.write('<!-- #Include file=vert_nav1/vert_nav1.html -->');
}
</script>

I believe I could do this pretty easily using ASP and VB, but in this
instance, I would prefer to use javascript and html.

However, although I do not get any script errors, this is not working. Am I
simply trying to do something that is not possible?

TIA

Steve G
 
T

Thomas A. Rowe

In order to use the #Include, the page must be either .asp or .shtml, etc.,
second the #Include is a server-side statement and must be executed on the
server before the page is sent to the browser, not client-side, so it will
never work in JavaScript.

--

==============================================
Thomas A. Rowe (Microsoft MVP - FrontPage)
WEBMASTER Resources(tm)

FrontPage Resources, WebCircle,
MS KB Quick Links, etc.
==============================================
 
S

Steve

Thanks Thomas...I "think" I knew that <G> is there a javascript equivalent
to all of this?

Steve
 
T

Thomas A. Rowe

I am not aware of any.

--

==============================================
Thomas A. Rowe (Microsoft MVP - FrontPage)
WEBMASTER Resources(tm)

FrontPage Resources, WebCircle,
MS KB Quick Links, etc.
==============================================
 
M

MD Websunlimited

Steve,

That code will not work as the include needs to be process at the server and JavaScript is client side.

To accomplish create two script files that write out the code using document.write statements. Next, use the conditional JavaScript
source that was shown before to src the included script.

To recap you'd have two file of script:
vert_nav.js and hor_nav.js

Then the page would contain:

<script language="Javascript" type="text/javascript" >
if (screen.width <= 600) {
document.write('<script src="side_nav/vert_nav.js" type="text/javascript" >');
}
else {
document.write('<script src="side_nav/hor_nav.js" type="text/javascript" >\n');
}
document.write('</script>');
</script>


In J-Bots we use a technique to allow browser independance while allowing ease of program. To accomplish this we use function
overlays that bring in code based upon the browser. This is the code that we use:

var oBrowser=new checkBrowser();

// load the browser specific code Why does this work?
if(oBrowser.ns4) {
document.write("<script language=\"JavaScript1.2\" src=\"" + sWUScripts + "_WUScripts/ns4obj.js\" >\n</" + "script>");
} else {
document.write("<script language=\"JavaScript\" src=\"" + sWUScripts + "_WUScripts/ieobj.js\" >\n</" + "script>");
}

if (oBrowser.ns5){
document.write("<script language=\"JavaScript\" src=\"" + sWUScripts + "_WUScripts/ns5obj.js\" >\n</" + "script>");
}


HTH,
 
S

Steve

Thanks Mike..this is a big help

Steve G

MD Websunlimited said:
Steve,

That code will not work as the include needs to be process at the server and JavaScript is client side.

To accomplish create two script files that write out the code using
document.write statements. Next, use the conditional JavaScript
source that was shown before to src the included script.

To recap you'd have two file of script:
vert_nav.js and hor_nav.js

Then the page would contain:

<script language="Javascript" type="text/javascript" >
if (screen.width <= 600) {
document.write('<script src="side_nav/vert_nav.js" type="text/javascript"
');
}
else {
document.write('<script src="side_nav/hor_nav.js" type="text/javascript" >\n');
}
document.write('</script>');
</script>


In J-Bots we use a technique to allow browser independance while allowing
ease of program. To accomplish this we use function
overlays that bring in code based upon the browser. This is the code that we use:

var oBrowser=new checkBrowser();

// load the browser specific code Why does this work?
if(oBrowser.ns4) {
document.write("<script language=\"JavaScript1.2\" src=\"" + sWUScripts
+ "_WUScripts/ns4obj.js\" >\n said:
} else {
document.write("<script language=\"JavaScript\" src=\"" + sWUScripts +
 
M

MD Websunlimited

Steve,

On thing that may help in your efforts here is to create a variable to hold the HTML, eg,

<script>
HTML='test test test';
HTML +='test1 test1 test1';
HTML +='test2 test2';
document.write(HTML);
</script>

Use F&R to replace any single quote in the HTML with an escaped single quote \'
Use a RegEx to add the HTML += ' begining of the line and '; to the end of the line.

--
Mike -- FrontPage MVP '97-'02
http://www.websunlimited.com
Stop Spam Email Mining from your web pages with SpamStopper
http://www.websunlimited.com/order/product/SpamStopper/spam_stopper_help_dir.htm
FrontPage Add-ins Since '97 2003 / 2002 / 2000 Compatible
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top