...

View Full Version : timed javascript shows on one page but not on another



llizard
12-20-2011, 10:34 PM
Knowing just enough about javascript to get myself in trouble, I have put three timed javascripts to appear from 18 December to the 1 January on three pages of my sister's website. The scripts are on the main index for the site; the index page for the bibliography folder /bibliography/ and in the education folder on just one page: /education/educationlinks.html.

Part one of the script inserts the css file; part two of the script inserts a "snowing" script and part three inserts a "make it stop snowing/let it snow" script. Everything is working perfectly for the main index page and the bibliography page. But I cannot figure out why the 3rd "make it stop snowing/let it snow" refuses to show up on the page in the education folder (the snowing javascript IS working on all three pages).

There are no errors being thrown and the education page validates.

This is the coding for the 3rd script:


// snowscript part 3: Show the script between 19-31 Dec and 1 Jan

var date = new Date();
var d = date.getDate();
var m = date.getMonth() + 1;
dm = d + m;

if (m = 12)
{
if (d > 18)
{ document.write('<script src="/scripts/snowlinkswitch.js" type="text/javascript" id="snow"></script>');
}
else
{
document.write('');
}
}
else if (dm == 1,1)
{
document.write('<script src="/scripts/snowlinkswitch.js" type="text/javascript"></script>');
}
else
{
document.write('');
}

This is the coding for snowlinkswitch.js


function myhref(){
document.getElementById('myAnchor').innerHTML="please let it snow!";
document.getElementById('myAnchor').href=location.href;
document.getElementById('myAnchor').title="let it snow! let it snow!";
document.getElementById('myAnchor').onclick="location.reload(); startstop('spanstyle'); return false;";
}

//

document.write ('<div id="snowlinks"><img src="/images/snow3aa.gif" width="11" height="11" border="0" alt="*"> <a id="myAnchor" href="" onclick="myhref();hidesnow(); return false;" title="please make it stop snowing!">please make it stop snowing!<\/a> <img src="/images/snow3aa.gif" width="11" height="11" border="0" alt="*"></div> ');

I would search for a similar occurrence but have no clue about what search words to use. Does anyone here have an idea about what I have done incorrectly that is causing this to happen? Many thanks.

E Morris
Toronto, Canada

wolfenr
12-21-2011, 04:29 PM
Can't say that I've thoroughly understood your problem but it may be something to do with your else if (dm == 1,1) statement.

I tested this by putting an alert(dm); statement early on and, as d & m are both returning integers, your dm = d + m; statement returns dm = 33 for todays date i.e. d=21, m=12 so d+m = 33.

So...
I suspect that your else if (dm == 1,1) statement is trying to test for a string i.e. 1st Jan so you probably need to be converting d & m into strings and use substring tests to check - string.substring(from, to)

Hope that gets you off in the right direction
R

Dormilich
12-21-2011, 05:07 PM
This is the coding for snowlinkswitch.js


function myhref(){
document.getElementById('myAnchor').innerHTML="please let it snow!";
document.getElementById('myAnchor').href=location.href;
document.getElementById('myAnchor').title="let it snow! let it snow!";
document.getElementById('myAnchor').onclick="location.reload(); startstop('spanstyle'); return false;";
}

I wouldn’t expect that to work at all (maybe except for IE). the onclick property expects a function to be given. reason being that it does not map the HTML attribute onclick, but the JavaScript Event handling system.

llizard
12-22-2011, 07:02 AM
Thank you for your replies, wolfenr and Dormilich. But I'm not sure that either one answers my problem though. (My knowledge of javascript coding is on the shaky side.)

If the script wasn't working at all, I'd start trying to understand the dm== thing and try using strings instead of integers. But the strange thing is that the script is working just fine on two out of three pages it appears on. The script works in both Firefox and IE on those two pages.

Is it at all possible that another javascript on the page would interfere with snowlinkswitch.js?

The snowlinkswitch script is working on the index page of peacemakers.ca. It is NOT working on /education/educationlinks.html where there is a random link javascript in place. (Please note that I have purposely left these pages unlinked.)

Philip M
12-22-2011, 08:28 AM
Is it at all possible that another javascript on the page would interfere with snowlinkswitch.js?

The snowlinkswitch script is working on the index page of peacemakers.ca. It is NOT working on /education/educationlinks.html where there is a random link javascript in place. (Please note that I have purposely left these pages unlinked.)

Short answer - yes, that is almost certainly your problem.

There are two reasons why multiple scripts in the same page will not work together.

a) duplicate variable and/or function names (including loop counters etc.)
b) multiple onload statements.
Have a look at:-
http://www.javascriptkit.com/javatutors/multiplejava.shtml
http://www.dyn-web.com/tutorials/combine.php

llizard
12-22-2011, 04:14 PM
There are two reasons why multiple scripts in the same page will not work together.

a) duplicate variable and/or function names (including loop counters etc.)
b) multiple onload statements.
Have a look at:-
http://www.javascriptkit.com/javatutors/multiplejava.shtml
http://www.dyn-web.com/tutorials/combine.php


Thank you Philip M. I have stared at both the links provided as well as http://www.javascriptkit.com/javatutors/eventaction4.shtml

As far as I can tell, essentially being a javascript paster rather than coder, I don't have an onload conflict.

How do I go about searching the two scripts for which function name(s) are conflicting and causing the snowscripting3 not to appear? (I'm guessing that the culprits are the snow script and the random link script on the education page.)

E Morris

Philip M
12-22-2011, 04:25 PM
How do I go about searching the two scripts for which function name(s) are conflicting and causing the snowscripting3 not to appear? (I'm guessing that the culprits are the snow script and the random link script on the education page.)

E Morris

As far as I know the only method is manual (or visual!) inspection.

Try removing your random link script to see if the snow thing works then. If so you have identified the culprit. If not, then the snow script is at fault.

Just noticed if (m = 12)
= is assignment. == is comparison.

Have you studied the comments in post #2 and post #3? And are you quite sure that exactly the same script is working on other pages? To me it would appear to have several serious flaws which have been pointed out to you.

Here is an example of code to do something between certain dates:-


var eventDate1 = new Date("December 19, 2011");
var eventDate2 = new Date("January 5, 2012");
if ((today >= eventDate1) && (today <= eventDate2)) {
document.write('<script src="/scripts/snowlinkswitch.js" type="text/javascript" id="snow"></script>');
}

I would use that instead of the stuff you have.

llizard
12-22-2011, 05:53 PM
And are you quite sure that exactly the same script is working on other pages?

Yes, I'm absolutely sure the same scripts are being used.

Thank you for the fix on the date part of the script. As you can probably guess, I had no idea how to achieve it and thought I had guessed correctly when the scripts worked.

I'll make the suggested changes and report back.

llizard
12-22-2011, 09:03 PM
I changed the coding for getting the script to show up between certain dates (thank you for that; it is MUCH less unwieldy than the one I cobbled together on my own) and as soon as adding
var today = new Date(); it works beautifully. (Of course, now I'll have to search again about how to show dates in javascript so I can understand how to reproduce something similar....)


As far as I know the only method [for searching two scripts for which function name(s) are conflicting] is manual (or visual!) inspection.

Try removing your random link script to see if the snow thing works then. If so you have identified the culprit. If not, then the snow script is at fault.

Well, rats. As suggested, I removed the random link script and the snowswitch.js still didn't work on the education page. (It continues to work on the other two pages.)

I also discovered that I had brilliantly (cough) put id="snow" on two of the script links. I removed one of the ids....

I'm guessing that the problem has to do with a conflict in "getElementById". This is in the snow script:


document.getElementById("dot"+i).style.top=yp[i]+"px";
document.getElementById("dot"+i).style.left=xp[i] + am[i]*Math.sin(dx[i])+"px";

This is in the snowswitch script:


function myhref(){
document.getElementById('myAnchor').innerHTML="please let it snow!";
document.getElementById('myAnchor').href=location.href;
document.getElementById('myAnchor').title="let it snow! let it snow!";
document.getElementById('myAnchor').onclick="location.reload(); startstop('spanstyle'); return false;";
}

Is this a hidden (from me) onload conflict? If so, how do I resolve it?

I've googled as best I can and alas have failed miserably. It seems to be a case of having to know the answer before asking the question.

-E Morris, who can't get over that the scripts are working just fine on two pages but refuse to load on one page.

Philip M
12-23-2011, 08:33 AM
Well, how precisely is the code on the third page different from the code on the other two pages where it works?

I think you will find that it is due to a simple typo or something in the calling page. Or something like the duplicate id's you mentioned.

Unless of course you have yet another script on the third page which you have not mentioned.

llizard
12-24-2011, 05:14 AM
Well, how precisely is the code on the third page different from the code on the other two pages where it works?

I think you will find that it is due to a simple typo or something in the calling page. Or something like the duplicate id's you mentioned.

Unless of course you have yet another script on the third page which you have not mentioned.

The code on the third page is virtually the same as the other pages except that it has the random link javascript (which doesn't appear to be causing the conflict) and a open link in new window javascript. As far as I can see, there are no typos, duplicate ids or duplicated variables.

The only duplications that I see are the mentioned getElementById's in the snow script and in the snowswitch script.

This is the new window javascript:


/* many thanks to the folks at http://www.codingforums.com/
for fixing this script so that it works properly

this is a popup javascript
*/

function openLink(objLink) { //to do with onclick=
var w=window.open(objLink, 'popup', 'toolbar=1,menubar=1,location=1,scrollbars=1,resizable=1,width=790,height=590,left=10,top=10');
w.focus(); // makes popped window open on top of other windows
return false;
}

// cause each link to open in the same window

function opensetAttribute(objLink){ //to do with mouseover=

var addStr = " \(opens in a popup\)"; // make sure there is a leading space

if (objLink.title.indexOf(addStr)==-1) {
objLink.title = objLink.title + addStr; //adds extra phrase to title in mouseover message
('target','_blank');
}
}

llizard
12-24-2011, 06:02 AM
Well. How very embarrassing. Please ignore my rash statement that "there are no typos". It turns out that the pieces of coding for the snowscripts were placed in the wrong parts of the page. It's actually miraculous that any portion of the snowing was working at all!

Please accept my apologies for the time wasted. And many many thanks for fixing the date section of the script, as well as helping me to understand about duplicate variables.

E Morris (where IS the red-faced smiley when I need it?)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum