I'm looking to generate a dynamic URL and insert it into an HTML tag that then gets added to the page using the .innerHTML property.
for (n = 0; n <= allImages.length - 1; n++) { // This cycles through all the images on the page.
function getUrl() {
var srcUrl = document.images[n].src;
return srcUrl; // Then I get the src URL for each image and return it.
}
var newP = document.createElement('p');
newP.innerHTML = document.images[n].naturalWidth + " x " + document.images[n].naturalHeight + " " + '<a href= \'javascript:window.location=getUrl();\'>LINK</a> '; // Run the 'getURL' function so the word LINK directs the viewer to the specific image URL.
imgUrlDiv.appendChild(newP); // Append the code each time to a new <p>
}
The issue I'm running into is that since the getURL function is added as the value for the 'href' it has to be in quotes and so it gets turne into a string (my guess) and so it gets rendered as is and doesn't pull the correct image URL.
Is there a way to insert the getURL function into the value of the href and have it render the link to each image on the page?
<a href= \'javascript:window.location=getUrl();\'>LINK</a>
These are some of the resources I've looked through but haven't been able to resolve this:
- Set iframe url based on dynamic Url
- Which "href" value should I use for JavaScript links, "#" or "javascript:void(0)"?
Thanks so much in advanced!