Forum Moderators: open

Message Too Old, No Replies

HTML link tag inside javascript

         

alex100

2:48 pm on Apr 15, 2008 (gmt 0)

10+ Year Member



Hi there,

I would like to know whether it would be possible to include an HTML tag inside a javascript, like this:

<script language='JavaScript' type='text/javascript'>
<!--

javascript code here...

<a href="page.html">HTML link inside javascript</a>

javascript code here...

// -->
</script>

I do not want to use the document.write function to output the HTML link tag. The A HREF tag must be hardcoded in the HTML page.

The reason I want to do this is because I want google.com to index that link and transfer some PR to the target page. If I used document.write, that would not happen, at least from what I know.

Thanks,
Alex

Fotiman

3:15 pm on Apr 15, 2008 (gmt 0)

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



Alex, have you heard of "Unobtrusive JavaScript"? It means your content will be nice and clean, and all of your scripting (which is just enhancement, not a requirement) will be stored in its own file. It sounds to me like you are currently mixing in script elements with your markup. Yuck.

I'm not sure what your script is going to be doing, but essentially you should place your script in its own file and include it at the end of your document. For example:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset:utf-8">
<title></title>
</head>
<body>
<a href="page.html">HTML link NOT inside javascript</a>
<script type="text/javascript" src="example.js"></script>
</body>
</html>

And yes, you should avoid document.write. Also, get rid of the language attribute... it's not valid (nor is it needed), and there's no need to wrap your script code in HTML comments (that was to support Netscape 1, which is long dead).

alex100

3:54 pm on Apr 15, 2008 (gmt 0)

10+ Year Member



Sorry, I forgot to tell you all that I'm new to javascripting, just a few days since I've started to search the net on how to get things done.

Anyway, I understand what you're saying but I doubt it can help me, and here's why:

I'm trying to build a small calendar that is formed by two parts:

1. HEADER
2. FOOTER

In between these two, I will need to have the A HREF link I was talking about. In the header there will be some HTML code to create the calendar header, then there will be the link, and then the HTML code of the footer will follow. It's all in this order and I can't change anything, like placing the link before the header and the footer, or after them. So the link must be in the middle, otherwise it will mess up my calendar design.

The code i'm using now is the one below. Please notice the last variable named 'part', which is set to either 'header' or 'footer'. This outputs the data in the exact order I need it: header, link and then footer.

<script language='JavaScript' type='text/javascript'><!--
var time=new Date();
var cm=time.getMonth();
var cd=time.getDay();
var cdn=time.getDate();
document.write("<script language='JavaScript' src='");
document.write("http://server/cgi-bin/calendar.pl?cm="+cm+"&cdn="+cdn+"&cd="+cd+"&part=header'></script>")
//--></script>

<a href="link.htm">HTML Link</a>

<script language='JavaScript' type='text/javascript'><!--
var time=new Date();
var cm=time.getMonth();
var cd=time.getDay();
var cdn=time.getDate();
document.write("<script language='JavaScript' src='");
document.write("http://server/cgi-bin/calendar.pl?cm="+cm+"&cdn="+cdn+"&cd="+cd+"&part=footer'></script>")
//--></script>

This is the only way I could get this done. Please note that I also need all the date variables to be passed to the Perl script, like it's shown above.

If you can think of more elegant way to do this (with less code!), please let me know.

Thanks,
Alex

Fotiman

8:18 pm on Apr 15, 2008 (gmt 0)

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



The large problem here is that you're using document.write. Also note that because you're writing the script tags out like this, any user with JavaScript disabled will not see your calendar.

A better way to do this would be to use a server-side language (PHP, ASP, etc.) to write out the script tags (or find a better calendar... this one seems a bit hokey to me if you have to execute it twice passing in different parameters for head and foot).

alex100

9:21 pm on Apr 15, 2008 (gmt 0)

10+ Year Member



The calendar is supposed to be installed remotely, on other web sites/pages. I just provide the code and whoever wants to put my calendar on his web site just inserts the code in his html page(s). And the calendar must show up in that place when pages are loaded in browsers.

If it was installed on the local server I would have used SSI and Perl and that would work with minimal code writing.

But this is not the case, unfortunately ;)

I was thinking of a solution like this:

<script language='JavaScript' type='text/javascript'>
<!--

(code here...)
document.write("<script language='JavaScript' src='");
document.write("http://server/cgi-bin/calendar.pl?...&part=header'></script>")

<a href="page.html">HTML link inside javascript</a>

(code here...)
document.write("<script language='JavaScript' src='");
document.write("http://server/cgi-bin/calendar.pl?...&part=footer'></script>")

// -->
</script>

This, of course, doesn't work, because of the HTML tag in the javascript.

But if it would, I would have accomplished all of my requirements:

1. use a single javascript within the web page;
2. have the link in plain HTML, hardcoded in the page, perfect for google.com to index;
3. follow the HTML flow that I want, in this particular order: header, HTML link, then footer.

I know it looks strange to you, but I need it all in this way. If I change the order sequence of the HTML code, it won't allow me to build the calendar the way I need it to look like. This is a must!

Is there a working solution to accomplish all this and still have a single javascript on a web page and not two? Like I said, it's duable with two javascripts, but how about with only one? What do you think?

Alex

Fotiman

3:17 pm on Apr 16, 2008 (gmt 0)

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



The way you are approaching this, no, it's not possible with only a single script block.

Here is how I would approach this if I was you.

1. Define the markup like this:


<div id="calendarContainer">
<a href="page.html">HTML link</a>
</div>

2. Include your single script tag (preferably at the bottom of the page just before your closing </body> tag):


<script type='text/javascript'>
document.write("<script src='");
document.write("http://server/cgi-bin/calendar.pl?target=calendarContainer&...'><\/script>");
</script>

Note, I removed that header/footer nonsense. Instead, have your generated script code look for the element passed in as target ('calendarContainer'). Then find the <a> element within that and insert the header content before that element using DOM methods (insertBefore), and append the footer to the end of target (appendChild). This would:
1. use a single javascript within the web page
2. have the link in plain HTML, hardcoded in the page
3. Use more pure markup, separating the script from the markup
4. Use the more appropriate DOM methods for adding content to the page instead of using document.write (except, of course, for the part that uses document.write to add your script to the page with some variables attached)
5. Would create your desired sequence of generated HTML code

But of course, it requires that your script for the calendar is updated to use the DOM methods instead of document.write, but you should really be doing that anyway. As I said before, avoid document.write.

Fotiman

3:48 pm on Apr 16, 2008 (gmt 0)

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



Here's a quick example I threw together. It's only purpose really is to demonstrate how to create a script that doesn't use document.write to generate content in an existing page.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset:utf-8">
<title></title>
</head>
<body>
<div>...Page Header...</div>
<div id="calendarContainer">
<a href="http://example.com/page.htm">Static Link</a>
</div>
<div>...Footer...</div>
<script type="text/javascript">
// Create a single object to contain all of our script
// so we don't muck up the global namespace
var MIGHTYCALENDAR = function () {
return {
/**
* Create our own addEvent function which we'll use to call
* our createCalendar method when window.onload is triggered
*/
addEvent : function (obj, type, fn) {
if (obj.attachEvent) {
obj['e'+type+fn] = fn;
obj[type+fn] = function () {
obj['e'+type+fn](window.event);
}
obj.attachEvent('on'+type, obj[type+fn]);
} else {
obj.addEventListener(type, fn, false);
}
},
createCalendar : function () {
// Get the calendar container, and links within it
var cc = document.getElementById('calendarContainer');
var aList = cc.getElementsByTagName('a');
var myLink = null;
var i;
// Find 'our' link
for (i = 0; i < aList.length; i++) {
if (aList[i].href && aList[i].href == 'http://example.com/page.htm') {
myLink = aList[i];
break;
}
}
if (!myLink) {
// Did not find our link, so return without doing anything
return;
}
// Generate calendar header
var h = document.createElement('div');
h.appendChild(document.createTextNode('Calendar Header'));
cc.insertBefore(h, myLink);
// Generate calendar footer
var f = document.createElement('div');
f.appendChild(document.createTextNode('Calendar Footer'));
cc.appendChild(f);
}
};
}();
MIGHTYCALENDAR.addEvent(window, 'load', MIGHTYCALENDAR.createCalendar);
</script>
</body>
</html>

alex100

5:30 pm on Apr 17, 2008 (gmt 0)

10+ Year Member



<div>...Page Header...</div>

<div>...Footer...</div>

The HTML data in both page header and footer is not supposed to be static, it's generated on the server side. The footer is the part that shows different events for different dates, and all of these events are written in a database on the server. In my example I used document.write because it has the ability to retrieve this data from the server and display it. I used:

document.write("<script language='JavaScript' src='");
document.write("http://server/cgi-bin/calendar.pl?...&part=footer'></script>")

This automatically fetches the data from the server, which is the HTML code of the footer. Inside this HTML code there's also the text event information I was talking above, which is mostly what the user will see in the footer of the calendar.

The same happens with the header, only the data there changes less frequent.

I should have added this requirement to my list above, didn't think we'd come here.

This is not the typical calendar showing the days of the month only, it actually shows these events that are taking place on the day the calendar is loaded, plus some other information that is almost always different.

Like I said, I'm new to JS, so please correct me if I'm wrong, but I guess all data inside the <div> </div> tags above is static and can't be retrieved from the server every time the calendar is loaded in the browser. Right? I'm just asking, I don't know...

Anyhow, please bear with me, I'm not really a programmer. Just trying to do my things by myself, as much as I can at least... ;)

Thank you,

Alex

Fotiman

3:23 pm on Apr 21, 2008 (gmt 0)

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




The HTML data in both page header and footer is not supposed to be static,

You totally misunderstood my point. In my example, the "page header" and "footer" were not part of the calendar. Those were only place holders in my example to show the rest of the page (not related to the calendar) so that you could see the content before the calendar and after it, so that when the calendar code is generated you can verify that it appears in the correct places.

Fotiman

3:29 pm on Apr 21, 2008 (gmt 0)

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



Why do you need to pass in the current day to your server side script? Just have the server side script get the current day. As I told you, your script sounds like a bit of a mess. I would ditch what you have and start with something like what I have pasted in this thread.