/    Sign up×
Community /Pin to ProfileBookmark

Hello, i was reading a book that attempts to teach javascript. I have not gotten very far on it but i saw this one example which i have tried various times but have not gotten it to work properly. It is mentioned in the book that you can have an html link that goes into one website while having a javascript that gets the id of the link and uses a function in order to redirect to another website. I imagine the intention is to have users with javascript disabled to be redirected to one while users with javascript to go to another page. The problem is that when i click the link the javascript function isnt enabled, therefore, i am just directed to the page placed on the html link tag.

these are the codes:
<html>
<head>
<sript language=”JavaScript”
type=”text/javascript”
src=”practice2.js”>
</script>
</head>
<body>
<a href=”http://www.yahoo.com” id=”redirect”>hello</a>

</body>

</html>
—————————————

window.onload initAll
function initAll() {
document.getElementById(“redirect”).
onclick = initredirect;

function initredirect() {
window.location = “http://www.google.com
return false;
}

In these examples i just used yahoo and google. Please tell me what i am doing wrong i have typed it over and over. Thanks for the help in advance.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@mikemDec 26.2008 — Hi,

Try amending your script like this...

[CODE]function initredirect()
{
location = "http://www.google.com";
}

function initAll()
{
document.getElementById("redirect").onclick = initredirect;
}

onload=initAll;[/CODE]
Mike ?
Copy linkTweet thisAlerts:
@NedalsDec 27.2008 — You have a lot of different things going on in that script.

Calling an external script.

Creating an event handler.

Performing the redirect.

While learning, I would suggest you deal with one issue at a time.
<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;

&lt;html&gt;&lt;head&gt;&lt;title&gt;untitled&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;
&lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;

&lt;script type="text/javascript"&gt;
/* 1. */
function redirect() {
location.href = "http://www.google.com"
return false;
}

/* 2.
function init() {
var el = document.getElementById("redirect");
el.onclick = function() {
location.href = "http://www.google.com"
return false;
}
}
window.onload = init
*/
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;!-- 1.
Note the 'return' in the onclick event (which returns false).
If javascript is enabled, the 'anchor' will be disabled and the redirect will be via the script.
--&gt; <br/>
&lt;a href="http://www.yahoo.com" onclick="return redirect()"&gt;hello&lt;/a&gt;

&lt;!-- 2.
&lt;a href="http://www.yahoo.com" id="redirect"&gt;hello&lt;/a&gt;
--&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@mikemDec 27.2008 — Hi again,

Yep, I omitted the false return from the onclick event-handler, which should be:

[CODE][COLOR="darkgreen"]// Define an event-handler function[/COLOR].
function initredirect()
{
[COLOR="DarkGreen"] // Load a specified URL and return false to disable the HTML link action.[/COLOR]
location = "http://www.google.com";
[COLOR="Red"]return false;[/COLOR]
}

[COLOR="darkgreen"]// Define an initialization function to be called when the load event fires.[/COLOR]
function initAll()
{
[COLOR="darkgreen"]// Nominate a function to call when the click event of a specified element fires.[/COLOR]
document.getElementById("redirect").onclick = initredirect;
}

[COLOR="DarkGreen"]// Nominate a function to call when the load event fires.[/COLOR]
onload=initAll;[/CODE]
[code=html]<a href="http://www.yahoo.com" id="redirect">hello</a> [/code]Mike ?
Copy linkTweet thisAlerts:
@WebnerdDec 27.2008 — On a separate note, NEVER post to the forum with a thread title of "question". Most everyone has a question that posts here. Try to make your subject more relevant to the topic of discussion and you can get more qualified responses.
Copy linkTweet thisAlerts:
@webmasterDAFDec 27.2008 — HOW TO START A NEW POST???

i loged in , and want to make a new post , but i cant find that option ,, thank you
Copy linkTweet thisAlerts:
@mikemDec 27.2008 — Hi webmasterDAF, Scroll down the table of posted threads then click the "New Thread" button at the bottom left of the table to start a new post. Mike?
×

Success!

Help @pwntastic spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...