Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FireFox Javascript function problem

Status
Not open for further replies.

chrisdl

Programmer
Jun 28, 2001
22
US
This html page works fine in IE but in FireFox it says func1 is not defined. In FireFox you can open the JavaScript console under the tools menu to see JavaScript errors.

---------------------------------------------------

<HTML> <HEAD> </HEAD>
<BODY>

<SCRIPT LANGUAGE="JavaScript" >

function func1()
{
document.write("func <br>");
}

function Main()
{
document.write("Testing<br>");
func1();
document.write("--END--");
}

</SCRIPT>

<form name="Form1"">
<input type="button" value="Go!" onClick="Main()">
</form>

</BODY> </HTML>


-Chris (Consultant / Software Engineer)
 
You have a completely contrived page and code here. What are you trying to accomplish in REAL life?

Lee
 
I was working on a more complex javascript program that was working in IE. I reduced the problem to it's simplest form. This very simple program does not work on FireFox. I need to get this concept to work to do many parts of my larger project. Can you get this to work on your version of FireFox?
I tried 2 computers just to see if I had a bad install. I would not work on both.

Any ideas?


-Chris (Consultant / Software Engineer)
 
If you provide contrived code, you won't get real answers to your problems. The code you provided didn't work on Firefox on my computer, but using document.write after a page is already written isn't normal. The page that's written by the first function has no <script> tags in it as soon as the first document.write blows away the previous HTML code. This could well be the problem, but it's really not worth much time chasing down problems in imaginary code.

Lee
 
I think you're being unfair. The code is obviously not imaginary - since it's right there where we all can see it. It may not be the code used on the actual project, but if the EXAMPLE code reproduces the problem, and is considerably simpler, then that's a GOOD THING. Obviously this EXAMPLE code does reproduce the problem, since you indicated that it didn't work, just like chrisdl said it doesn't. If you can't get that simple example to work, what makes you think that you would have better luck with a larger and more complex piece of code?


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
My point is that Javascript using document.write() on a page that's already loaded will blow away the original page, and will have side effects depending on how the browser handles Javascript. There are no practical real world examples that I'm aware of where client-side JS is used to do what this example does. I know I could be wrong, but in my experience haven't run across anything where rewriting the entire page from scratch from within the page is appropriate.

I've written large portions of pages with JS, written entire popup windows pages with JS, and rewritten <div> and other containers from scratch on a page. However, the example provided isn't anything like I've seen or done (on purpose) since I've been using JS. Not having seen the REAL code, there's no way I can evaluate whether the method being used is prone to problems like this, whether the programmer is trying to do something that should be done another way, or what. Part of experience is being able to discern whether something is good practice or not, not just how to implement any old idea is presented. With contrived code like the original post, there's no way to see what the actual intent is or if there's a better way to accomplish the desired results.

Lee
 
This shouldn't work. Document.write is not dynamic. You call the document.write function from the form after the whole page has been loaded and rendered. It won't work.

Again, what are you trying to do? do you want to see if func1() is actually called? Then just ad window.alert("testing func1"); to function one, that will be called when func1 is trigger, but don't expect your page to be rewritten dynamically. That doesn't happen.
 
Reducing the problem is a useful thing to do. Once you get the example working, you have a chance with the real problem.

But I think that Trollacious has found the problem. My guess is that once Main() is called, document.write("Testing <br>") destroys the code. Essentially your page now reads "Testing <br>" and nothing else. In other words, you've also destroyed the function func1.

I'm only new to JavaScript, but I've had to re-write pages dynamically. I too tried to write(), but failed because the whole page goes. But you can change individual elements.

Try this instead:

<HTML> <HEAD> </HEAD>
<BODY>

<SCRIPT LANGUAGE="JavaScript" >

function func1(doc)
{
return doc + "func <br>";
}

function Main(){
var doc2 = "testing <br>";
doc2 = func1(doc2);
doc2 += "--END--";
document.body.innerHTML=doc2;
return true;
}
</SCRIPT>

<form name="Form1">
<input type="button" value="Go!" onClick="Main()">
</form>

</BODY> </HTML>

Alternatively, you might find insertAdjacentHTML() to be a handy method.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top