Forum Moderators: open

Message Too Old, No Replies

Javascript works in FF 3.6 but not in IE8

Browser and Javascript Issues

         

joakimkasper

7:41 pm on Mar 17, 2010 (gmt 0)

10+ Year Member




Hello!

I have a problem with a javascript function that retrieves the username upon login to a website I made. The code works in mozilla firefox 3.6 but not in IE8

Any help would be greatly appreciated. Thank you!

Best, Kasper

The coding follows below...

The code in my .htm file:
<script type="text/javascript" src="#*$!xx.js">



The code in the #*$!xx.js file:
function getusername()
{
if (document.cookie.length<0) return '';
begin=document.cookie.indexOf('mtusr=');
if (begin==-1) return '';
begin=begin+6;
end=document.cookie.indexOf(';',begin);
if (end==-1) end=document.cookie.length;
username=unescape(document.cookie.substring(begin,end));
return username;
}
document.write(getusername());

daveVk

10:30 pm on Mar 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Check if it works using

alert(getusername());

in place of

document.write(getusername());

document.write is not a good debugging tool.

Welcome to the forum.

joakimkasper

12:10 am on Mar 18, 2010 (gmt 0)

10+ Year Member



Hi Dave

Thank you for replying to my post!

What I need is not an alert popup message. I need javascript to write the username within the html.

In my html I have:
Welcome&nbsp;<script language="javascript" type="text/javascript" src="tt.js"></script>!


The js coding I placed in a seperate js file as I mentioned above.

Is there another way to do this or is document.write the only way?

Thank you/Kasper

daveVk

5:49 am on Mar 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome&nbsp;<span id=userNameSpan></span>!

and

document.getElementById( 'userNameSpan' ).innerHTML = getusername();

ps
<script language="javascript" type="text/javascript" src="tt.js"> then goes in the head section, and the above statement needs to be executed on page load. This assumes username does not contain odd characters that need html excaping.

Fotiman

1:28 pm on Mar 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




then goes in the head section,

Actually, it's become more standard to place all scripts at the end of the body for performance reasons, just before the closing </body> element.

joakimkasper

7:11 pm on Mar 18, 2010 (gmt 0)

10+ Year Member



Thanks Dave, you are brilliant. It now works in IE.

But....

Now it doesn't work right in Firefox 3.6 :(

Is there some way I can have my old script run only when the Browser is FF and when the user browser is IE, my website runs your script? In this way, my site would be compatible with both IE and FF. Just seems to me that this may be the only way to achieve cross browser compatibility.

The situation now following your advice:

In IE the username is now displayed correctly!

Right now in the header I have:
<script language="javascript" type="text/javascript" src="tt.js"></script>
<script language="javascript" type="text/javascript">
document.getElementById( 'userNameSpan' ).innerHTML = getusername();</script>


And in the html, I have:

Welcome&nbsp;<span id="userNameSpan"></span>!


Thanks/Kasper

Fotiman

7:37 pm on Mar 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



@joakimkasper, as was pointed out, this needs to be executed on page load. They way you've included it, it will just execute immediately, before the page has finished loading. It shouldn't work in any browser.

Move your script tags out of the <head> and put them just before the closing </body> tag and it should work.

joakimkasper

7:39 pm on Mar 18, 2010 (gmt 0)

10+ Year Member



Oh, I was wrong. It now doesn't work correctly in either browser.

Did I follow your advice correctly Dave?

Any help anyone?

Thank you/Kasper

joakimkasper

7:52 pm on Mar 18, 2010 (gmt 0)

10+ Year Member



Fotiman, that did the trick - thank you soooo much!

One thing though:

In Firefox: Perfect!

In IE: Username is not displayed immediately after login. If I login and refresh the website, then the username is there. Why is it not displayed immediately? Any way to fix this?

Again, thanks a million - I'm almost there now...

Best, Kasper

daveVk

12:19 am on Mar 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Username is not displayed immediately after login


How you handle login ?

If there some javascript involved try adding
document.getElementById( 'userNameSpan' ).innerHTML = getusername(); after cookie has been set;

Fotiman: Do you have a good reference on the advantages putting the script at the end ?

Seems counter intuitive to me, particularly if there a lot of script to load prior to page ending (displaying?).

Fotiman

2:17 am on Mar 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



@daveVk
[developer.yahoo.com...]
Basically, when browsers download a script file, they will block any other downloads, giving the appearance of a slower loading site, especially if there are a lot of scripts. By moving scripts to the end, it allows the page to start rendering faster. Douglas Crockford talks about it in "The Theory of the DOM [yuiblog.com]" (a 3 part video series on the Yahoo Theater site).

daveVk

4:22 am on Mar 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Fotiman: Thanks, "While a script is downloading, however, the browser won't start any other downloads" strange design decision !

Fotiman

1:26 pm on Mar 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



@daveVk, think of it like this. Imagine if you have multiple script files that do document.write. If the browser loads multiple script files in parallel, there's no way to know which document.write would occur first. One file might depend on the execution of the other file (for example, perhaps one files defines objects that are then accessed in the other). If the script files were loading in parallel, you can't ensure that your dependencies will load BEFORE the script(s) that need them.

joakimkasper

4:12 pm on Mar 19, 2010 (gmt 0)

10+ Year Member



Fortiman, would really appreciate feedback to this one I posted above: Thanks! :)

Fortiman, that did the trick - thank you soooo much!

One thing though:

In Firefox: Perfect!

In IE: Username is not displayed immediately after login. If I login and refresh the website, then the username is there. Why is it not displayed immediately? Any way to fix this?

Again, thanks a million - I'm almost there now...

Best, Kasper

Fotiman

5:02 pm on Mar 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I was waiting to see how you answered daveVk's question:
How you handle login ?

Do you set the value in the cookie without reloading the page? It would help to get a better understanding of what you are doing.

joakimkasper

6:38 pm on Mar 19, 2010 (gmt 0)

10+ Year Member




Hey Fortiman

Okay, I understand...

The way my login works is it puts a cookie on the computer which stays throughout the session.

The login software I use puts a login shield on the start page (the one you get to right after login).

The software encrypts the start page until login is complete.

From the outset I have 2 htm files. A login page and the start page. The login software merges the 2 files into 1 file. When login is complete, only the start page is left. The login part goes away.

Therefore, I believe that no refresh takes place upon login. In Firefox 3.6 it works perfect though. But in IE8, I have to refresh myself before the username comes up.

Can I command a page refresh upon login?

Again, thank you dave and fortiman - I really appreciate your help!

Best, Kasper

joakimkasper

6:40 pm on Mar 19, 2010 (gmt 0)

10+ Year Member




The coding in my tt.js file which handles the cookie is here:

[/code]
function getusername()
{
if (document.cookie.length<0) return '';
begin=document.cookie.indexOf('mtusr=');
if (begin==-1) return '';
begin=begin+6;
end=document.cookie.indexOf(';',begin);
if (end==-1) end=document.cookie.length;
username=unescape(document.cookie.substring(begin,end));
return username;
}
document.write(getusername());
[code]

Fotiman

6:45 pm on Mar 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It could be a timing issue. When your login part "goes away", is there an event associated with this or a callback method that executes that removes the login piece? You may want to tie in the getusername() call to whatever is running at that time. It's hard to guess without seeing some of the code that runs when the login part is removed.

Fotiman

6:47 pm on Mar 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




document.write(getusername());

I don't think you want to use document.write, as was pointed out above. document.write can't execute after the page has finished loading, else it will totally replace your existing page/document with only the piece that you write.

joakimkasper

6:53 pm on Mar 19, 2010 (gmt 0)

10+ Year Member



Fortiman, is there something better than document.write that I can use?

It works perfect in Firefox but in IE I have to refresh myself after login. That's why I was thinking I could command a page refresh...

Fotiman

7:01 pm on Mar 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



is there something better than document.write that I can use?

Yes, the example that was posted earlier in this thread:

document.getElementById( 'userNameSpan' ).innerHTML = getusername();

joakimkasper

9:18 pm on Mar 19, 2010 (gmt 0)

10+ Year Member



Great that did it!

You guys rock...

Thanks a lot, Kasper :)

daveVk

10:31 pm on Mar 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the script files were loading in parallel, you can't ensure that your dependencies will load BEFORE the script(s) that need them.

This assumes the load order and execution order are the same, that may be the case, but need not be.