Webmaster Key - Discussion Forums


Welcome, Guest. Please login or register.
Did you miss your activation email?
February 09, 2012, 10:26:12 AM

Login with username, password and session length
Welceome to Forums!

Important information for guests and new members:

In order to understand the full benefits of becoming an active member of this forum, please review the following information on guest and new member restrictions. These forum changes have been prompted by an overwhelming and unreasonable amount of bot postings and incoherent guest spam messages. We wish to prevent these events from happening in the future and make our community a more comfortable place for all of our members.

For guests:

Guests are not allowed to open new topics, polls, or posts attachments.
If you wish to open up new discussions on this forum, we encourage you to register.

For new members:

New members with less than five posts are not allowed to modify additional profile information such as avatars, contact information, biographies, and signatures. However, new members are encouraged to post their own topics or reply to topics initiated by other members. Become active on the forums and 5 posts should be an easy task!

We are a diverse community with members from all over the world. We encourage new ideas and interesting conversation. Do not be afraid to post webmaster/computer-related questions or problems, as our active members are always willing to help when they are able. Interested? Join us.

+ Webmaster Key Forums
|-+ Webmaster Corner
| |-+ Site Design and Web Authoring
| | |-+ reference javascript from external file doesn't work
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] Go Down Stumble Upon! Digg It! del.icio.us! Add to Technorati! ReddIt!  Send this topic Print
Author Topic: reference javascript from external file doesn't work  (Read 2810 times)
websj06
Jr. Member
*
Posts: 31


« on: September 27, 2006, 05:08:17 PM »

Dear Forum Members:

  I try to show a status bar message by refering JavaScript from an external file. The status bar message won't show when I view html file in IE browser/FireFox browser. Could you take a look my code and tell me what's went wrong? Thanks!

Here is the code used in my html file to refer to external JavaScript file. (I also tried file extension as status_barMessage.js. It won't work either)
<script src="status_barMessage.htm" type="text/javascript"></script>


my script code look like this:

<html>


<head>
<title>hompage layout</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<script language="JavaScript" type="text/javascript">

var frontPart, backPart;
var myMsg = "Welcome to California National Park Page!";
var i = 0;

function scrollMsg() {
frontPart = myMsg.substring(i, myMsg.length);
backPart = myMsg.substring(0, i);

window.status = frontPart + backPart;

if (i < myMsg.length) i++;
else i = 0;

setTimeout("scrollMsg()", 200) ;
}

</script>

</head>


<body style="background-color: #99CC99;" onload="scrollMsg();">

</body>
</html>

###################################################################3
My html file source code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>

<html>
<!--
Author: Jeff
-->

<head>
<title>California National Parks</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="keyword" content="national park, rewood national park, yosemite, death valley" />
<meta name="description" content="california national, highlights, park information, hiking" />

</head>

<body style="color: #996600; font-family: Times, Helvetica, sans-serif; background-image: url(images/creamparch.jpg);">

<!-- link to javascript file ** doesn't work -->

<script src="status_barMessage.htm" type="text/javascript"></script>

<h2>Welcome to COIN63!</h2>
</body>
</html>

Jeff
9-27-06

Report to moderator   Logged
Andy
Administrator
Veteran
*****
Posts: 5 752



« Reply #1 on: September 27, 2006, 11:11:35 PM »

Hi Jeff,

I think the problem is that your external file is not a Javascript file, it's an html file. You should only have javascript code in this file because it's effectively cutted and pasted into your web page at the script insertion point.

So your external file should only contain this code:

Code:
var frontPart, backPart;
var myMsg = "Welcome to California National Park Page!";
var i = 0;

function scrollMsg() {
frontPart = myMsg.substring(i, myMsg.length);
backPart = myMsg.substring(0, i);

window.status = frontPart + backPart;

if (i < myMsg.length) i++;
else i = 0;

setTimeout("scrollMsg()", 200) ;
}

Save the file as status_barMessage.js i.e. not using a .htm extension.

In your html page, refer to the script like this:
<script src="status_barMessage.js" type="text/javascript"></script>

Now the javascript code should be inserted correctly. Note, that I didn't test your code so it may have some bugs.

Andy
Report to moderator   Logged

websj06
Jr. Member
*
Posts: 31


« Reply #2 on: September 28, 2006, 06:15:16 AM »

Hello Andi:

  Thanks for pointing out my error. I changed file extension to .js. But The status bar message won't display. I couldn't fingure out what went wrong! Could you help me to test the code and tell me what's wrong? Thanks!

  In FireFox browser JavaScript Console show one error. It says, "Missing } in XML expression"

 the error points to --> frontPart = myMsg.substring(i, myMsg.length); (I didn't see anything wrong with this line of the code)

  here are the code from my two files:

  a) external JavaScript File
Code:
<script language="JavaScript" type="text/javascript">

 var frontPart, backPart;
 var myMsg = " Welcome to California National Park Page!";
 var i = 0;

 function scrollMsg()
 {
      frontPart = myMsg.substring(i, myMsg.length);
      backPart = myMsg.substring(0, i);

      window.status = frontPart + backPart;

      if (i < myMsg.length) i++;
      else  i = 0;

      setTimeout("scrollMsg()", 200) ;
}

</script>

b) HTML file that refers to external JavaScript file
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

<head>
   <title>California National Parks</title>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
   <meta name="keyword" content="national park, rewood national park, yosemite, death valley" />
   <meta name="description" content="california national, highlights, park information, hiking" />

</head>

<body style="color: #996600; font-family: Times, Helvetica, sans-serif; background-image: url(images/creamparch.jpg);">

<!-- link to javascript file ** doesn't work -->

 <script src="status_barMessage.js" type="text/javascript" language="JavaScript" ></script>

  <h2>Welcome to COIN63!</h2>
 </body>


</html>

Jeff
9-27-06

 
Report to moderator   Logged
Andy
Administrator
Veteran
*****
Posts: 5 752



« Reply #3 on: September 28, 2006, 02:12:30 PM »

Quote
So your external file should only contain this code:

You added some extra lines.

Plus you didn't call the actual function that does the scrolling.

Here is the working code for the external .js file Grin

Code:
var frontPart, backPart;
var myMsg = "Welcome to California National Park Page!";
var i = 0;

scrollMsg();

function scrollMsg() {
frontPart = myMsg.substring(i, myMsg.length);
backPart = myMsg.substring(0, i);

window.status = frontPart + backPart;

if (i < myMsg.length) i++;
else i = 0;

setTimeout("scrollMsg()", 200) ;
}

p.s. it will only work in Internet Explorer (or others?) since Firefox doesn't let Javascript add text to the status bar as far as I know.

Andy
« Last Edit: September 28, 2006, 02:54:51 PM by Andy » Report to moderator   Logged

websj06
Jr. Member
*
Posts: 31


« Reply #4 on: September 28, 2006, 04:17:45 PM »

Hello Andy:


   I took off the <script> tag in .js file and added onLoad=scrollMsg(); in <body> tag of html file. This time it worked Smiley.

 Thanks for you help! I now know how to reference a function in an external JavaScript file.

Jeff
9-28-06
Report to moderator   Logged
Andy
Administrator
Veteran
*****
Posts: 5 752



« Reply #5 on: September 29, 2006, 02:22:50 PM »

Hi Jeff,

it was kind of fun to get it working. This is a cool script, shame it doesn't work in Firefox. But IE is most popular.

Glad to be of assistance  Smiley

p.s. did you notice I added the call to the function in the .js file outside of the function definition? You don't have to have it in the onload event.

Andy
Report to moderator   Logged

Pages: [1] Go Up Stumble Upon! Digg It! del.icio.us! Add to Technorati! ReddIt!  Send this topic Print 
+ Webmaster Key Forums
|-+ Webmaster Corner
| |-+ Site Design and Web Authoring
| | |-+ reference javascript from external file doesn't work

Jump to:  
« previous next »


Our Partners
RelmaxTOP Ranking System Web Hosting RelmaxTOP Ranking System
Staff Sites
12Noon[12Noon Gallery] Andy[Urgentclick]
Tamuril[Tamuril's Digital Art Exhibit] Sensovision
Powered by MySQL Powered by PHP We are hosted by Relmax Inc. |Our Privacy Policy | Sitemap
Powered by SMF 1.1.9 | SMF © 2006-2009, Simple Machines LLC
Forum design by Tamuril © 2005.
Valid XHTML 1.0! Valid CSS!