PDA

View Full Version : javascript for firefox



comperr
05-31-2007, 08:09 PM
I have this code


musi="couldyou.mid"
musi2="maybeyoucan.mid"
function mus1()
{
if (t.options.selectedIndex==0)
{
mus.innerHTML="<embed src="+musi+" loop=-1>"
}
if (t.options.selectedIndex==1)
{
mus.innerHTML="<embed src="+musi2+" loop=-1>"
}
if (t.options.selectedIndex==2)
{
mus.innerHTML=""
}
}

and I can't seem to find a firefox alternate to this code.

charlesgan
05-31-2007, 09:48 PM
i have a small .js script that only run in IE. but not firefox.
i already give up on them :)

comperr
06-01-2007, 09:47 AM
how do you edit html using JS in FF? thats all I really need to know.

felgall
06-01-2007, 01:41 PM
Firefox like all other browsers provides two ways to update the HTML.

The preferred and standard compliant way is to use the Document Object Model commands.

The quicker way also cross browser supported is to use innerHTML.

The one browser that works differently from the others is Internet Explorer because IE doesn't understand JavaScript but instead processes the code as JScript. Fortunately for most commands inside the web page the two are the same or have known differences that can be handled by commonly available function calls.

IE is the most restrictive of the browsers when it comes to using innerHTML. There are lots of things that Firefox, Opera, and Safari allow you to do that don't work with IE.

The main thing is to avoid browser specific code that only runs in some browsers such as document.all, <embed>, <marquee> and all the JScript specific commands designed for intranets that provide access outside of the browser.

charlesgan
06-01-2007, 08:49 PM
how do you edit html using JS in FF? thats all I really need to know.

.js?? i just paste all the content of the javascript to a new .js file.
and call this file from the html page. so, it will reuse the code.
**but still, this wont work with firefox. even a simple javascript.

js tutorial here
http://www.w3schools.com/js/default.asp

comperr
06-02-2007, 09:40 PM
The quicker way also cross browser supported is to use innerHTML.


I dislike using DOM because older browsers won't support it - but they will support innerHTML. Anyway - why won't the code above work then if FF allows innerHTML?

Pethens
06-02-2007, 09:46 PM
The code you posted is difficult to debug because it doesn't look like it should work in any browser. Try posting more of your code, if there is more.

comperr
06-02-2007, 10:14 PM
<html>
<head>

<SCRIPT LANGUAGE="JavaScript">
musi="couldyou.mid"
musi2="maybeyoucan.mid"
function mus1()
{
if (t.options.selectedIndex==0)
{
mus.innerHTML="<embed src="+musi+" loop=-1>"
}
if (t.options.selectedIndex==1)
{
mus.innerHTML="<embed src="+musi2+" loop=-1>"
}
if (t.options.selectedIndex==2)
{
mus.innerHTML=""
}
}
</script>

<link rel="stylesheet" type="text/css" href="../tmc.css">
</head>
<body>
<small><i>What music would you like to hear?</i></small>
<script>
document.write("<span id='mus' style='position:absolute;top:-90000'></span>")
</script>

<select name="t" onchange=mus1()>
<option>couldyou.mid</option>
<option>MaybeYouCan.mid</option>
<option selected="selected">Disable music</option>
</select>
</body>
</html>

Pethens
06-03-2007, 10:41 AM
I rewrote your code to work in Explorer, Firefox, and Opera. You may have to tweak it to get the behavior you want. You really need to read a good book about Javascript that explains how to use the DOM properly. And don't rely on things that work in Explorer, that will teach you to write bad code.



<html>
<head>

<SCRIPT LANGUAGE="JavaScript">
musi="couldyou.mid";
musi2="maybeyoucan.mid";
function mus1(theselect)
{
var mus = document.getElementById("mus");
if (theselect.options.selectedIndex==0)
{
mus.innerHTML="<embed src='"+musi+"' loop=-1>";
}
if (theselect.options.selectedIndex==1)
{
mus.innerHTML="<embed src='"+musi2+"' loop=-1>";
}
if (theselect.options.selectedIndex==2)
{
mus.innerHTML="";
}
}
</script>

</head>
<body>
<small><i>What music would you like to hear?</i></small>
<span id='mus'></span>

<select name="t" onchange=mus1(this)>
<option>couldyou.mid</option>
<option>MaybeYouCan.mid</option>
<option selected="selected">Disable music</option>
</select>
</body>
</html>

comperr
06-03-2007, 02:11 PM
I know JS really well. I dislike using the DOM because my target audience are users who likely have windows 98 computers with old an old IE that doesn't support DOM changes. That's why I didn't write it the way you did.
I think I may just use that for FF and lave the old code for IE only....

comperr
06-03-2007, 02:15 PM
woops - just looked @ ur code - thanx...I thought you did something else...sorry

felgall
06-03-2007, 03:19 PM
I dislike using the DOM because my target audience are users who likely have windows 98 computers with old an old IE that doesn't support DOM changes.


IE5.0 supports the DOM so I guess that means that most of the world's IE4 users are visiting your site just about exclusively and not going anywhere else on the web apart from your site (since the stats for browser versions that don't support the basic DOM commands are so close to zero that you usually need to look at several months worth of stats at once to see it even show up in the list of browsers at all).

comperr
06-03-2007, 05:51 PM
Lets just say that the target audience probably needs help just to use a mouse - let alone use the internet. I want to allow for as many users and possible. This music thing is part of a tutorial on how to use forms....

Pethens
06-03-2007, 10:28 PM
You can go back as far as Explorer 4 by changing this line:


var mus = document.getElementById("mus");

to this:


var mus;
if (document.all) {
mus = document.all("mus");
} else {
mus = document.getElementById("mus");
}

I wouldn't know how to do this with Netscape 4 but I suppose you could try...

comperr
06-04-2007, 03:48 PM
thanks for all your replies. As you can probably tell I am used to working with much more modern browsers....