Click to See Complete Forum and Search --> : Newbie: Javascript Links
qbalance
09-13-2003, 07:18 PM
I've no programming background so I'm way over my head with Javascript. But I would like to use it for some outbound links that are not reciprocated or that will not do much for my PR.
I've got the following code
I put this at the top of my page
<script language="javascript"><!--
function go(loc){
self.location="http://www."+loc
}
//-->
</script>
and this is the link
<a href="javascript:go('qbalance.com/CPA_tips_contact_numbers_and_sites.htm')">phone numbers</a>
My questions are:
1. qbalance.com is my website. Do I need to type this or can I just type the page CPA_tips_contact_numbers_and_sites.htm
2. I use target="_blank" to have a link come up with a new page in html. How would I modify the above javascript so that a new window opens when a link leaves my website?
Thanks for your help.
Linda Saltz
soccer362001
09-13-2003, 07:21 PM
Originally posted by qbalance
My questions are:
1. qbalance.com is my website. Do I need to type this or can I just type the page CPA_tips_contact_numbers_and_sites.htm
2. I use target="_blank" to have a link come up with a new page in html. How would I modify the above javascript so that a new window opens when a link leaves my website?
1. http://www.qbalance.com
2.<a href="" target="new">
xataku_nakusute
09-13-2003, 07:23 PM
1) no, you still must keep the qbalance.com part in order for the link to go to the correct page.
2) by 'leave my website'...do you mean a link whose root domain is not qbalance.com? or what...?
qbalance
09-13-2003, 07:29 PM
'leave my website'...do you mean a link whose root domain is not qbalance.com?. yes. lets say I want to link to the
american institute of CPAs.
My goal is when someone comes to my site, clicks on a link to someone else's site, a new window pops open. When done, they will close that window, and still have an opportunity to continue visiting my site.
I'm very very new at this. I appreciate your help
Linda
soccer362001
09-13-2003, 07:36 PM
<a href="" target="new">This will open a new window</a>
xataku_nakusute
09-13-2003, 07:38 PM
well, id just make plain and simple like this:
<script language="javascript"><!--
function go(aID, dom, page){
if(dom=="qbalance.com/")
{
document.getElementById(aID).target="_top";
self.location="http://www."+dom+loc
else
{
document.getElementById(aID).target="_blank";
self.location="http://www."+dom+loc
}
//-->
</script>
and this is the link
<a id="link1" href="javascript:go('link1','qbalance.com/','CPA_tips_contact_numbers_and_sites.htm')">phone numbers</a>
qbalance
09-13-2003, 07:52 PM
Hi,
I pasted this code into a page and it came up with an error
here:
self.location="http://www."+dom+loc
else
also, I dont understand
id="link1
do i need to give each link a number? and then
put it into the javascript?
Do I start with the number 1 for each new
page of my website?
Thanks
Linda
xataku_nakusute
09-13-2003, 08:09 PM
ok, first off, lemme clear up that error:
<script language="javascript"><!--
function go(aID, dom, page){
if(dom=="qbalance.com/")
{
document.getElementById(aID).target="_top";
self.location="http://www."+dom+page
}
else
{
document.getElementById(aID).target="_blank";
self.location="http://www."+dom+page
}
}
//-->
</script>
and this is the link
<a id="link1" href="javascript:go('link1','qbalance.com/','CPA_tips_contact_numbers_and_sites.htm')">phone numbers</a>
and second off, lemme explain to ya the 'reazning' of the code:
ok, when we call the function, we call it like so within your <a> tag:
href="java script:go('link1','qbalance.com/','CPA_tips_contact_numbers_and_sites.htm')">
prolly the part youre most confused with is this:
('link1','qbalance.com/','CPA_tips_contact_numbers_and_sites.htm')
so ill break it down for ya:
'link1' is saying that the element with an id of 'link1' should be checked to see whether or not to open in a new window.
'qbalance.com/'
has been assigned to the 'dom' variable. this is the first part of the url. this is the part that is checked within the link to see if its within your site or not.
and
'CPA_tips_contact_numbers_and_sites.htm'
is the page you want to go to within the site 'qbalance.com/'.
so, within the javascript function i have this:
function go(aID, dom, page){
this says when you call the function, you should have 3 parameters: the links id, the domain url, and the page url, in that order exactly.
if(dom=="qbalance.com/")
{
document.getElementById(aID).target="_top";
self.location="http://www."+dom+page
}
this says that if the variable 'dom'(the domain url) is the same as 'qbalance.com/', then the link should have a target value of '_top'(aka the same window). and "http://www."+dom+page means it should redirect the page to the variable 'dom' and the variable 'page' put together.
else
{
document.getElementById(aID).target="_blank";
self.location="http://www."+dom+page
}
}
this says, if the above code is false(if 'dom' doesnt equal 'qbalance.com/'), then the links target value should become '_blank'(new window). and then it should redirect again as before.
any more questions?
qbalance
09-13-2003, 08:33 PM
Thanks for the explanation.
I now understand what that javascript is saying.
But I will repeat just to make sure
For every link that has an ID= link1,
the javascript with perform the task of matching
up the url qbalance.com to see if a new
window or the same window should be used.
Lastly, I think there is still a problem with the code
I pasted the code into a test page
http://www.qbalance.com/test_copy(1).htm
If you click on the link "phone numbers" from this test page. It does not go to the intended page. Instead it ends up on the sitemap, (but is ignoring the css file )
because I have the instructed the server to point any page
that a user types in which is not an existing page
to redirect to the sitemap.
Here is what the sitemap should look like
http://www.qbalance.com/sitemap.htm
Here is where the link "phone number" should have ended up
http://www.qbalance.com/CPA_tips_contact_numbers_and_sites.htm
I really really appreciate your help
Linda Saltz