最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - dynamic link based on your current url - Stack Overflow

programmeradmin1浏览0评论

I know this will sound odd but stay with me on this.

I am trying to create a link that will dynamically add on ending string of the current url.

Example:

you are looking at an open window with a url of www.yahoo/(*lucky.aspx*)

and the link on the page reads

<a href="www.lucky/(replace with just *lucky.aspx* url)[Google analytic code]">LOOK A LINK</a>

The problems I am faced with is that I need to shave down everything before the "/" and preserve everything after the .aspx

The code I have so far goes like this

<script type="text/javascript">
            window.onload = function() { 
                var grabedurl = window.location.href
                document.getElementById('url').value=grabedurl; 
            }
</script>
<a href="www.lucky/(some element id=url)[Google analytic code]">LOOK A LINK</a>

Thanks for any help you can offer.


Updated code

<script type="text/javascript">
  window.onload = function() { 
    var grabedurl = window.location.pathname + window.location.search;
    document.getElementById('url').src = ".php?href=http%3A%2F%2Fwww.example"+grabedurl+"&amp;layout=button_count&amp;show_faces=false&amp;width=100&amp;action=like&amp;font=arial"; 
  }
</script>
<iframe id="url" src="" style="overflow: hidden; border: 0px none; width: 90px; height: 25px;"></iframe>

I know this will sound odd but stay with me on this.

I am trying to create a link that will dynamically add on ending string of the current url.

Example:

you are looking at an open window with a url of www.yahoo./(*lucky.aspx*)

and the link on the page reads

<a href="www.lucky./(replace with just *lucky.aspx* url)[Google analytic code]">LOOK A LINK</a>

The problems I am faced with is that I need to shave down everything before the "/" and preserve everything after the .aspx

The code I have so far goes like this

<script type="text/javascript">
            window.onload = function() { 
                var grabedurl = window.location.href
                document.getElementById('url').value=grabedurl; 
            }
</script>
<a href="www.lucky./(some element id=url)[Google analytic code]">LOOK A LINK</a>

Thanks for any help you can offer.


Updated code

<script type="text/javascript">
  window.onload = function() { 
    var grabedurl = window.location.pathname + window.location.search;
    document.getElementById('url').src = "http://www.facebook./plugins/like.php?href=http%3A%2F%2Fwww.example."+grabedurl+"&amp;layout=button_count&amp;show_faces=false&amp;width=100&amp;action=like&amp;font=arial"; 
  }
</script>
<iframe id="url" src="" style="overflow: hidden; border: 0px none; width: 90px; height: 25px;"></iframe>
Share Improve this question edited Jun 8, 2011 at 22:05 webmaster alex l asked Jun 8, 2011 at 18:51 webmaster alex lwebmaster alex l 6634 gold badges17 silver badges32 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

I'm not pletely sure what you are asking, but would this be something you'd need?

<script type="text/javascript">
  window.onload = function() { 
    var grabedurl = window.location.pathname + window.location.search;
    document.getElementById('url').href = "http://www.lucky." + grabedurl + "[Google analytic code]"; 
  }
</script>
<a href="www.lucky.[Google analytic code]" id="url">LOOK A LINK</a>
发布评论

评论列表(0)

  1. 暂无评论