2

I have a html link reference something like:

click here<a href="http://www.example.com/usr/page.aspx?pgid=1">www.example.com</a>

Now I want to add javascript tags to make the www.example.com url to be dynamic. so it will always retrieve as

<a href="[DYNAMIC URL]/usr/page.aspx?pgid=1">www.example.com</a>

Can anyone please advice?TQ

7
  • Don't you have backend? What is the dynamic means in your terms? Commented Dec 5, 2014 at 9:22
  • 1
    Is that what you want <a href="/usr/page.aspx?pgid=1">www.example.com</a> ? Will be interpreted by browser with current hostname, protocol, port. Commented Dec 5, 2014 at 9:28
  • @dfsq yes, and i want the script to get any domain from the URL which the user visit. Means if the user visit site1.com. it will go to "site1.com/usr/page.aspx?pgid=1" Commented Dec 5, 2014 at 9:31
  • So try what I suggested, looks like this is your solution. Commented Dec 5, 2014 at 9:32
  • If you change your link to <a href="/usr/page.aspx?pgid=1">www.example.com</a>, when a user visits a page served by site1.com, the link will point to http|https://site1.com//usr/page.aspx?pgid=1 Commented Dec 5, 2014 at 9:46

3 Answers 3

1

Is this what you're looking for? Check JSFiddle Demo

HTML

<a class="dyna" href="[DYN]/usr/page.aspx?pgid=1">www.example.com</a><br/>
<a class="dyna" href="[DYN]/usr/page.aspx?pgid=1">www.site.com</a><br/>
<a class="dyna" href="[DYN]/usr/page.aspx?pgid=1">www.bla.com</a><br/>

JS

var l = document.getElementsByClassName("dyna").length;

for (var i=0; l > 0; l--){
    var inner = document.getElementsByClassName("dyna")[i].innerHTML,
        x = document.getElementsByClassName("dyna")[i].getAttribute('href'),
        y = x.replace('[DYN]','http://'+inner);

    document.getElementsByClassName("dyna")[i].setAttribute('href',y);

    i++
}

Note: I used a class to retrieve the anchors but you can look for TagName a if you prefer ...

Sign up to request clarification or add additional context in comments.

Comments

0

Using javascript get domainname and add it to create link you need.

var domain = document.domain;
var link = '<a href=\"'+domain+'/usr/page.aspx?pgid=1\">www.example.com</a>';
link // here you get a anchor tag html.

To get the domain name example.com from the set of possible subdomains sub1.example.com sub2.example.com sub3.example.com. you can try below code.

var parts = location.hostname.split('.');
var subDomain = parts.shift();
var upperLevelDomain = parts.join('.');

To get only the second-level-domain, you might use

var secondLevelDomain = parts.slice(-2).join('.');

Comments

0

The href attribute can be a relative1 path:

<a href="/path/to/page">Blah</a>

The link would grab the current protocol, host, and port and append whatever comes next.

Sample scenario: let's pretend the current URL is http://yoursite.com/pages/links.html the above example anchor would navigate when clicked to http://yoursite.com/path/to/page.

NOTE: If you omit the leading slash in the relative path

<a href="path/to/page">Blah</a>

The current full path will be used.

Sample scenario: let's pretende the current URL is http://yoursite.com/pages/links.html then the anchor without leading slash would navigate when clicked to http://yoursite.com/pages/path/to/page

1: relative as in relative to the current page

Comments

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.