| Coding Forum Problems with your code? Let's hear about it. |
01-02-2005, 07:01 AM
|
#1 (permalink)
|
|
Inactive
Join Date: 10-26-03
Posts: 2,466
Latest Blog: None
|
Simple JavaScript question
Hi there -
I have a piece of HTML code I want to insert into the main template of a site using Javascript, but I'm not quite sure what I need to do.
I figure I'll need to call up the script using something like:
Code:
<SCRIPT LANGUAGE="JavaScript1.4" SRC="/js/html.js" TYPE='text/javascript'></SCRIPT>
I tried putting the HTML code within:
Code:
document.write(' HTMLcode ');
and saving it as a .js document, but that didn't work.
As you can probably etll, I'm not at all used to JS, and am simply fumbling around here.
Any pointers on how to solve this little chestnut would be most appreciated.
My main requirement is to simply ensure that the JS file is called externally.
|
|
|
01-02-2005, 02:24 PM
|
#2 (permalink)
|
|
Inactive
Join Date: 10-13-03
Location: New Zealand
Posts: 534
Latest Blog: None
|
It looks like it should work. What exactly is the problem, e.g. does the script load and cause an error, or does the script not load at all?
|
|
|
01-02-2005, 02:31 PM
|
#3 (permalink)
|
|
Member
Join Date: 01-02-05
Posts: 45
Latest Blog: None
|
code looks ok. make sure SRC="/js/html.js" point to your javascript source code. for debug purpose, you can include your javascript into the html doc, instead of a source file.
|
|
|
01-02-2005, 04:51 PM
|
#4 (permalink)
|
|
Inactive
Join Date: 10-26-03
Posts: 2,466
Latest Blog: None
|
Does it make a difference if I put the code between the body tags at all?? I'm simply getting no effect - the HTML is simply not appearing and there is no error message at all.
|
|
|
01-02-2005, 08:01 PM
|
#5 (permalink)
|
|
Moderator
Join Date: 10-13-03
Location: UK
Posts: 2,819
Latest Blog: None
|
the first slash here means start form the root folder (of your space), did u mean to do that or is js a subfolder of the page?
|
|
|
01-02-2005, 08:05 PM
|
#6 (permalink)
|
|
Moderator
Join Date: 10-13-03
Location: UK
Posts: 2,819
Latest Blog: None
|
procedure for finding source of error, try html in the page (no JS) as suggested above, (if u can), assuming this works...
next try JS to insert a simple text like 'hello world' to check if you can do that, so the HTML u wanted to insert would be the problem.
if not try JS alert('ALERT!'); to see if the js is being run.
in the <body> yes! if you want to document.write() something in you page it wiouldn't be seen if outside of the body tag.
Last edited by LazyJim : 01-02-2005 at 08:08 PM.
|
|
|
01-02-2005, 11:44 PM
|
#7 (permalink)
|
|
v7n Mentor
Join Date: 02-18-04
Location: Minneapolis, Minnesota
Posts: 1,941
|
In the HTML file you want to use the code type the following (make sure you save the JS file in your JS folder):
<SCRIPT LANGUAGE="JavaScript1.4" SRC="/js/html.js" TYPE='text/javascript'></SCRIPT>
In the html.js file use ONLY (DO NOT use the SCRIPT tags in the JS file):
document.write('HTMLcode');
The other way to do it is to just add the javascript into your html file directly.
<SCRIPT LANGUAGE="JavaScript1.4" TYPE='text/javascript'>
document.write('HTMLcode');
</SCRIPT>
TIP: I would remove the 1.4 unless you are using ONLY 1.4 JavaScript code. Because older browsers will not work if they don't support JavaScript 1.4.
imaginemn
|
|
|
01-03-2005, 04:06 AM
|
#8 (permalink)
|
|
Inactive
Join Date: 10-26-03
Posts: 2,466
Latest Blog: None
|
Thanks for the help, all - just adding text within the document.write commas works fine - which suggests it to be an issue with the HTML code itself. It simply does not want to call up a table with cells via the Javascript. Are there special rules for handling this??
|
|
|
01-03-2005, 07:25 AM
|
#9 (permalink)
|
|
v7n Mentor
Join Date: 02-18-04
Location: Minneapolis, Minnesota
Posts: 1,941
|
Double check all your characters.
document.write('<table><TR><TD>How's my work?</TD></TR></TABLE>');
This will fail because of the extra ' character under "How's". If you need special characters (which is usually the problem) use a leading slash.
document.write('<table><TR><TD>How\'s my work?</TD></TR></TABLE>');
or
document.write('<table><TR width=\"600\"><TD>How\'s my work?</TD></TR></TABLE>');
You might want to post the code that is not working to see if there is something we can see to help you better.
imaginemn
|
|
|
01-04-2005, 07:55 AM
|
#10 (permalink)
|
|
Moderator
Join Date: 10-13-03
Location: UK
Posts: 2,819
Latest Blog: None
|
yup that means probably an escape problem.
|
|
|
01-04-2005, 07:58 AM
|
#11 (permalink)
|
|
Contributing Member
Join Date: 10-13-03
Location: Belgium
Posts: 322
Latest Blog: None
|
Quote:
|
Originally Posted by imaginemn
Double check all your characters.
document.write('<table><TR><TD>How's my work?</TD></TR></TABLE>');
This will fail because of the extra ' character under "How's". If you need special characters (which is usually the problem) use a leading slash.
document.write('<table><TR><TD>How\'s my work?</TD></TR></TABLE>');
or
document.write('<table><TR width=\"600\"><TD>How\'s my work?</TD></TR></TABLE>');
You might want to post the code that is not working to see if there is something we can see to help you better.
imaginemn
|
You forget document.write("<TABLE><TR WIDTH=600><TD>How's my work?</TD></TR></TABLE>"); 
|
|
|
01-05-2005, 01:28 AM
|
#12 (permalink)
|
|
Inactive
Join Date: 10-26-03
Posts: 2,466
Latest Blog: None
|
The HTML code I'm trying to Javascript is the DMOZ copyright code:
Code:
<TABLE BORDER="0" BGCOLOR="#666699" CELLPADDING="3" CELLSPACING="0"><TR><TD>
<TABLE WIDTH=100% CELLPADDING=2 CELLSPACING=0 BORDER=0><TR ALIGN=center>
<TD><FONT COLOR="#FFFFFF">Help build the largest human-edited directory on the web.</FONT></TD>
</TR><TR BGCOLOR="#DDDDDD" ALIGN=center>
<TD> <A HREF="http://dmoz.org/cgi-bin/add.cgi?where=$path">Submit a Site</A> - <A HREF="http://dmoz.org/about.html"><B>Open Directory Project</B></A> - <A HREF="http://dmoz.org/cgi-bin/apply.cgi?where=$path">Become an Editor</A> </TD>
</TR></TABLE>
</TD></TR></TABLE>
I'm building a site that calls on DMOZ data, but I figure that Google and friends may use the DMOZ footprint as a way to devalue pages using DMOZ data - something I am keen to avoid. Using external Javascript is one way that may circumvent that issue - for the time being at least.
However, even with forward slashes, I simply cannot get the DMOZ code to work in a Javascript code.
Is there something radical I'm overlooking, or am I just being sloppy?
|
|
|
01-05-2005, 02:26 AM
|
#13 (permalink)
|
|
Contributing Member
Join Date: 10-13-03
Location: Belgium
Posts: 322
Latest Blog: None
|
I don't understand why it doesn't work, but I tried it myself and with me it doesn't work either
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<SCRIPT LANGUAGE="javascript">
document.write("<TABLE BORDER=\"0\" BGCOLOR=\"#666699\" CELLPADDING=\"3\" CELLSPACING=\"0\"><TR><TD><TABLE WIDTH=100% CELLPADDING=2 CELLSPACING=0 BORDER=0><TR ALIGN=center><TD><FONT COLOR=\"#FFFFFF\">Help build the largest human-edited directory on the web.</FONT></TD></TR><TR BGCOLOR=\"#DDDDDD\" ALIGN=center><TD><A HREF=\"http://dmoz.org/cgi-bin/add.cgi?where=$path\">Submit a Site</A> - <A HREF=\"http://dmoz.org/about.html\"><B>Open Directory Project</B></A> - <A HREF=\"http://dmoz.org/cgi-bin/apply.cgi?where=$path\">Become an Editor</A> </TD></TR></TABLE></TD></TR></TABLE></SCRIPT></body></html>");
</SCRIPT>
</BODY>
</HTML>
this just prints "); onto the page without any error warning
DragonEye
|
|
|
01-05-2005, 07:55 AM
|
#14 (permalink)
|
|
v7n Mentor
Join Date: 02-18-04
Location: Minneapolis, Minnesota
Posts: 1,941
|
JavaScript does some funny things sometimes.
Try:
Code:
<SCRIPT LANGUAGE="javascript">
document.write('<TABLE BORDER="0" BGCOLOR="#666699" CELLPADDING="3" CELLSPACING="0"><TR><TD>');
document.write('<TABLE WIDTH=100% CELLPADDING=2 CELLSPACING=0 BORDER=0><TR ALIGN=center>');
document.write('<TD><FONT COLOR="#FFFFFF">Help build the largest human-edited directory on the web.</FONT></TD>');
document.write('</TR><TR BGCOLOR="#DDDDDD" ALIGN=center>');
document.write('<TD> <A HREF="http://dmoz.org/cgi-bin/add.cgi?where=$path">Submit a Site</A> - <A HREF="http://dmoz.org/about.html"><B>Open Directory Project</B></A> - <A HREF="http://dmoz.org/cgi-bin/apply.cgi?where=$path">Become an Editor</A> </TD>');
document.write('</TR></TABLE>');
document.write('</TD></TR></TABLE>');
</SCRIPT>
OR
Code:
<SCRIPT LANGUAGE="javascript">
document.write('<TABLE BORDER="0" BGCOLOR="#666699" CELLPADDING="3" CELLSPACING="0"><TR><TD><TABLE WIDTH=100% CELLPADDING=2 CELLSPACING=0 BORDER=0><TR ALIGN=center><TD><FONT COLOR="#FFFFFF">Help build the largest human-edited directory on the web.</FONT></TD></TR><TR BGCOLOR="#DDDDDD" ALIGN=center><TD> <A HREF="http://dmoz.org/cgi-bin/add.cgi?where=$path">Submit a Site</A> - <A HREF="http://dmoz.org/about.html"><B>Open Directory Project</B></A> - <A HREF="http://dmoz.org/cgi-bin/apply.cgi?where=$path">Become an Editor</A> </TD></TR></TABLE></TD></TR></TABLE>');
</script>
imaginemn
|
|
|
01-05-2005, 02:41 PM
|
#15 (permalink)
|
|
Inactive
Join Date: 10-13-03
Location: New Zealand
Posts: 534
Latest Blog: None
|
Quote:
|
Originally Posted by DragonEye
this just prints "); onto the page without any error warning
|
The code you posted has a rogue </script> tag which causes the script to abort prematurely. It also has questionable </body></html> tags.
|
|
|
01-06-2005, 04:56 AM
|
#16 (permalink)
|
|
Inactive
Join Date: 10-26-03
Posts: 2,466
Latest Blog: None
|
Superb work again, imaginenm. 
|
|
|
01-07-2005, 11:27 AM
|
#17 (permalink)
|
|
Contributing Member
Join Date: 03-06-04
Location: NY, CT, CA, AZ
Posts: 540
Latest Blog: None
|
I experimented and found that javascript works when I escape all reserved characters found in CGI/Perl.
__________________
"90% of the game is half mental."
--Yogi Berra
Elizabeth Arden
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -7. The time now is 04:39 AM.
© Copyright 2008 V7 Inc
|