Forum Moderators: open

Message Too Old, No Replies

Simple question from a js newbie

How to make a link in js to open in same window

         

Macro

4:52 pm on Dec 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I need to create some links that won't transfer Google PR(irrespective of the debate as to whether PR matters). I understand that Google reads some js but I understand that depending on how you do the javascript link you can avoid Google giving PR to the destination page.

I have tried something along the lines of
<a href="javascript:void window.open 'http://www.myownsite/archive1.htm','', '');"><font face="verdana,arial,helvetica" size="1">Archive Page 1</font></a>

But this open the link in a new window and I'd like it to be in the same window. Do I have to use something like document.write (of which I know nothing and will have to learn js just to do this)? I would appreciate if anyone can give me the code I'd need. Thanks

AWildman

5:13 pm on Dec 31, 2003 (gmt 0)

10+ Year Member



You could try
<a href="javascript:void; window.location.replace('http://www.myownsite/archive1.htm');"><font face="verdana,arial,helvetica" size="1">Archive Page 1</font></a>

Macro

5:39 pm on Dec 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



AWildman, thanks. That works a treat... except for one little thing. It seems to disable the Back button on the browser. Maybe I'm doing something wrong. I would appreciate any other suggestion.

AWildman

5:58 pm on Dec 31, 2003 (gmt 0)

10+ Year Member



Oops...I'm sorry. I didn't take into account that you wanted normal navigation thereafter. Replace removes the last location from the history. How about window.location.href = [myownsite...]

Macro

6:19 pm on Dec 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is the usage then as follows? (I am a complete js novice)

<a href="javascript:void; window.location.href('http://www.myownsite/archive1.htm');"><font face="verdana,arial,helvetica" size="1">Archive Page 1</font></a>

AWildman

6:45 pm on Dec 31, 2003 (gmt 0)

10+ Year Member



No, the last part would actually be window.location.href = www.yourdomain.com/page.htm. Everything else remains the same. Or, if you feel more comfortable with it, create an external js file and include the following:

function change_location(val)
{window.location.href = val;}

Okay, so how does that help? Well, it save tons of space in your html code cause then your anchor tags would only consist of <a href = "javascript: change_location("www.somelocation.com")>www.somelocation.com</a> AND you could use the same easy code throughout your site. In case you don't know, here's how to link the external js file:

<SCRIPT LANGUAGE="JavaScript" SRC="externaljsfilename.js" type = "text/javascript"></SCRIPT>

Macro

7:32 pm on Dec 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Now I'm completely confused :-(

I don't know what the "last part" is. It seems that js is very fussy. A wrong comma or brackets and everything gets screwed up. Is it:
<a href="javascript:void; window.location.href = mydomain.com/page.htm><font face="verdana,arial,helvetica" size="1">Archive Page 1</font></a> . That doesn't seem to work.

Maybe I exaggerated when I said I was a js novice. I meant I was a js complete idiot ;-)

>> Or, if you feel more comfortable with it, create an external js file
I'll have to pass on that. If I can't get this right it's unlikely I'll have much joy with that. Also, it appears that I'll have to create one external js for every destination page (if my guess is correct)

My simple solution would be a replacement for your original suggestion of
<a href="javascript:void; window.location.replace('http://www.myownsite/archive1.htm');"><font face="verdana,arial,helvetica" size="1">Archive Page 1</font></a>
...... with one that does not disable the back button

I appreciate your help.

DrDoc

7:45 pm on Dec 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is it just me... or is the whole JS thing waaay redundant?
Why do you need JS in the first place?

Why not just do this:

<a href="http://www.myownsite/archive1.htm"><font face="verdana,arial,helvetica" size="1">Archive Page 1</font></a>

Macro

8:04 pm on Dec 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why do you need JS in the first place?

Because, as stated earlier, I don't want to transfer PR to these new pages, and I don't want these new pages to be indexed by Google. It would be nice if these new pages were all conveniently in one directory, I could use a robots.txt to block them being indexed.

But as it stands now I need to do it via js.

garann

8:44 pm on Dec 31, 2003 (gmt 0)

10+ Year Member



Macro, I think your code isn't working because you didn't have quotes around the new URL. Javascript is probably under the impression that your new URL is a variable, which would certainly cause it to complain. Try this instead:

<a href="javascript:window.location.href='mydomain.com/page.htm';">

Macro

8:51 pm on Dec 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks garann, I'll give that a shot. Do you know if that does protect against transferring PR?

DrDoc

8:59 pm on Dec 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It would be nice if these new pages were all conveniently in one directory, I could use a robots.txt to block them being indexed.

You can still use robots.txt to disallow those pages.

User-agent: *
Disallow: foobar.html
Disallow: widgets.html
Disallow: barbaz.html
Disallow: path/to/some/file.html

I don't want these new pages to be indexed by Google

...instead you want to possibly prohibit users who may surf with JS disabled from accessing your pages?

Use the right tool for the right job ;)
robots.txt is the way to go... especially since googlebot actually obeys it.

Macro

9:19 pm on Dec 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



DrDoc, I really don't mind users without js not being able to see these pages (101% of the users who access these pages will have js anyway - long story). And I do not see it as practical to keep adding hundreds of files names to the robots.txt. At some point someone is going to forget that when they add a new page to the site they've got to update robots.txt.

With due respect - and I don't mean to argue with you - but I do believe that js is the right tool for this specific job. Thanks for understanding.

DrDoc

9:24 pm on Dec 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And adding the new pages to a different folder is not a feasible solution?

moonbather

2:56 am on Jan 1, 2004 (gmt 0)

10+ Year Member




<script>
<!--
mylinks=new Array()
mylinks[0]="http://www.google.com"
mylinks[1]="http://www.yahoo.com"
mylinks[2]="http://www.cnn.com"
linksdesc=new Array()
linksdesc[0]="Google"
linksdesc[1]="Yahoo"
linksdesc[2]="CNN"
for (i=0;i<mylinks.length;i++)
document.write('<a href='+mylinks[i]+'>'+linksdesc[i]+'<\/a><br />')
// -->
</script>