0

Silly question but I would like to create or set an image src that appends the current url after a string.

So I would like to write

<img src="//testURL.comaction?item={www.currenturl.com}" id="tabUrl" />

So {www.currenturl.com} will be replaced with the current URL

I know that I can get the current URL by using window.location.href; but how can I add this to a string?

Not sure if I should create a variable and apply it using document.getElementById

taboo = window.location.href;

Or do a document write similar to the example below

<script>document.write('<img src="'//testURL.comaction?item= + 
window.location.href + '">')</script>

Any help is appreciated.

1
  • 2
    What's wrong with the two approaches you've laid out? Do they not work? Commented Apr 18, 2017 at 17:08

2 Answers 2

1

Your quotes are a bit off: try this

'<img src=//testURL.comaction?item=' + window.location.href + '>'
Sign up to request clarification or add additional context in comments.

Comments

1

As you want a pure JS way to do the same, I would recommend following.

var string="testURL.comaction?item="+window.location.href;
document.getElementsByTagName("img").setAttribute("src", string);

1 Comment

I used your example and no changes were made to my code <img src="" width="0" height="0" style="display: none" id="tabUrl" />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.