Welcome to WebmasterWorld Guest from 54.226.67.166

Forum Moderators: open

Message Too Old, No Replies

How to get a JS script to work in all browsers

javascript and browsers

     

steveoh

2:37 pm on Jul 30, 2013 (gmt 0)



I copied a script from a site that made bats fly around the page. It only appeared to work on IE, but with trial and error found it would work on any browser if there was no doc-type declaration, and just <html> at the top of the page. Any ideas about a workaround so that it would work on all browsers, but with a valid doc-type?

DrDoc

3:51 pm on Jul 30, 2013 (gmt 0)

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Welcome to WebmasterWorld, steveoh!

Seems like this was asked recently? A brief sample of code would probably be helpful.

The short answer is -- the JavaScript you copied was (likely) poorly written. Getting something to work in all browsers isn't rocket science. It's simply lack of laziness.

Kudos for desiring a valid doctype. Again, a brief code sample would help.

Dijkgraaf

2:37 am on Jul 31, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about just trying the HTML5 standard?
<!doctype html>
<html lang="en">

Setting the language as desired of course

lucy24

2:53 am on Jul 31, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



with trial and error found it would work on any browser if there was no doc-type declaration

In other words: the code as written works only in "quirks" mode, so there is something non-compliant about it-- or, more likely, in the HTML it generates. Browsers will generally recognize HTML 5 features even if the dtd says HTML 4, so that shouldn't be the problem.

You'll need to fine-tooth-comb the code. Break it down into small bits, or comment-out most of it. You'll find some particular point where it stops working as intended. Once you've isolated the issue, you can work on fixing it. And you'll end up with better code.

birdbrain

7:23 am on Jul 31, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there steveoh,

one possible reason for failure is the lack of specified values. ;)

For example something like this...


someObject.style.top=xPos;



...will need to be changed to...


someObject.style.top=xPos+'px';



birdbrain

steveoh

2:58 pm on Jul 31, 2013 (gmt 0)



Hi guys,

Thanks very much for getting back to me.

I did ask a similar question recently, and that script did work in most browsers, but it was the HTML that was producing errors in W3 validation. That's sorted now.

I tried using <!doctype html> but it didn't make a difference.

Going through the code with a fine tooth-comb won't be of much use as I know next to nothing about JavaScript.

It's seems a shame that there isn't a quick fix as I liked the effect and it did work in all browsers, but I'll leave it. I can't expect you to rewrite someone else's code.

Thanks again for your time.

birdbrain

3:05 pm on Jul 31, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there steveoh,

post the script, and we will get it to work. ;)

birdbrain

steveoh

3:10 pm on Aug 8, 2013 (gmt 0)



Thanks for the invite.
I've put up the code that generates the HTML, as it was suggested that that may be causing the problems (I've changed some from upper to lower case but it didn't seem to improve it).

var ns6=document.getElementById&&!document.all
bats=new Array(3)
if (document.layers){
for (i=0; i < amount; i++)
{document.write("<layer name=n"+i+" left=0 top=-50><img src='"+Cupid.src+"' name='nsi' width='69' height='60' border='0'></layer>")}
}
else if (document.all||ns6){
document.write('<div id="out" style="position:absolute;top:0;left:0"><div id="in" style="position:relative">');
for (i=0; i < amount; i++){
if (document.all)
document.write('<img src="'+Cupid.src+'" id="msieBats" style="position:absolute;top:-50;left:0" border=0>')
else
document.write('<img src="'+Cupid.src+'" id="ns6Bats'+i+'" width=69 height=60 style="position:absolute;top:-50;left:0" border=0>')
}
document.write('</div></div>');
}

Fotiman

3:29 pm on Aug 8, 2013 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Anything that's looking for Netscape 6 is a sure sign of a script you should probably not use. It's using layer elements (non-standard junk) and document.write (which should be avoided). I'd find a new script. :/

The script that you posted does not seem to be complete, however.

DrDoc

3:36 pm on Aug 8, 2013 (gmt 0)

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Yikes, that's an old script!

That said --
top:-50
-- the unit is missing here. 50 what? Pixels? Ems? Inches? ;)

steveoh

3:38 pm on Aug 8, 2013 (gmt 0)



Thanks very much for this,
I didn't want to put up the whole script as it's quite lengthy
 

Featured Threads

Hot Threads This Week

Hot Threads This Month