fightstarr20
fightstarr20

Reputation: 12568

Pass link URL variable to javascript

I am trying to create a link that people can put on their own websites that links back to their profile on my site.

I have created the 'widget' using javascript so have a .js file that simplified does this...

document.write("Visit my profile at...");
document.write("    <a href=\"myprofilelink.html\">Click To View</a>");

I want to be able to create a call to it that automatically includes the profile link in the javascript so for example, something like....

<script src="widget.js" id="myprofilelink.html></script>

Can someone point me in the right direction?

Upvotes: 1

Views: 193

Answers (1)

Karl Laurentius Roos
Karl Laurentius Roos

Reputation: 4399

You could create a variable before including your widget script.

<script type="text/javascript">var profileLink = "myprofilelink.html";</script>
<script type="text/javascript" src="widget.js"></script>

Then you can just use the variable in your widget.js file:

document.write("<a href=\"" + profileLink + "\">Click To View</a>");

If you want to learn about how to work with more advanced JavaScript widgets I'd recommend reading Developing An Embeddable Javascript Widget.

Upvotes: 2

Related Questions