user2284703
user2284703

Reputation: 367

Appending one URL to another as a link

What I am trying to do (and failing miserably at it) is create a link... and that link should open up a specific page with the current page appended to it. For example... If the user is on a page called www.bar.com and click the link... he would go to www.foo.com?link=www.bar.com

Here is my sorry little attempt at this...

<img src="#" id="link" target="_blank">

<script type="text/javascript">
window.onload = function(){
    document.getElementById("link").href = 'http://www.foo.com?link='.window.location.toString();
}
</script>

I could do this in PHP but I can not use PHP for this particular page. Can someone please help?

EDIT: I completely botched up my question be cause I added code here that i was in the middle of editing on accident. I was trying to alter that a href code to place a specific image in that place instead ...for example

<img src="#"> 

becomes...

<img src="http://www.bar.com?link=www.foo.com">

Sorry for my ignorance.

Upvotes: 0

Views: 60

Answers (2)

jony89
jony89

Reputation: 5367

It's best to wrap your img with a tag and then change its attribute : (and use + and not . for chaining strings ! )

<a href="#" target="_blank" id="myLink"><img src="#" ></a>

<script type="text/javascript">
window.onload = function(){
    document.getElementById("myLink").href = 'http://www.foo.com?link='+ window.location.href.toString();
}
</script>

Upvotes: 2

user2138919
user2138919

Reputation:

Try this

    <img src="#" id="link" target="_blank">

<script type="text/javascript">
    window.onload = function(){
        document.getElementById("link").src = 'http://www.foo.com?link='+window.location.href;
    }
</script>

Upvotes: 1

Related Questions