te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>Using a Javascript variable in HTML <a> link - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Using a Javascript variable in HTML <a> link - Stack Overflow

programmeradmin2浏览0评论

My code goes something like this -

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" 
    ".dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title></title>


    <script type='text/javascript'>

    var random_generator;
    document.write('Random Selection: ');
    var arr=['One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten'];
    var randomnumber=Math.floor(Math.random()*10);
    random_generator=arr[randomnumber];
    document.write(random_generator+ '<br>');


    </script>

<style type="text/css">

</style>
<a href='www.xyz/Index1/Index2/Variable/Index4'>Good Morning</a>
</head>
<body> 
</body>  
</html>

In place of Variable in the tag, I want to use random_generator Javascript variable. How do I do that? Thanks in Advance.

NOTE: random_generator doesn't replace the entire link. Only a part of the link (/Variable/). Depending on this value, the user is shown different pages.

My code goes something like this -

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" 
    "http://www.w3/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title></title>


    <script type='text/javascript'>

    var random_generator;
    document.write('Random Selection: ');
    var arr=['One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten'];
    var randomnumber=Math.floor(Math.random()*10);
    random_generator=arr[randomnumber];
    document.write(random_generator+ '<br>');


    </script>

<style type="text/css">

</style>
<a href='www.xyz./Index1/Index2/Variable/Index4'>Good Morning</a>
</head>
<body> 
</body>  
</html>

In place of Variable in the tag, I want to use random_generator Javascript variable. How do I do that? Thanks in Advance.

NOTE: random_generator doesn't replace the entire link. Only a part of the link (/Variable/). Depending on this value, the user is shown different pages.

Share Improve this question edited Jan 3, 2013 at 15:54 user1238870 asked Oct 22, 2012 at 20:06 CafecorridorCafecorridor 1,6093 gold badges12 silver badges11 bronze badges 3
  • In place of the href or the text? Sorry I'm confused. – War10ck Commented Oct 22, 2012 at 20:07
  • In place of Variable in the href link. Different address link depending on the value of Variable ( /Variable/ ) in the href tag. – Cafecorridor Commented Oct 22, 2012 at 20:09
  • My apologies. I missed that. Thank you for your patience. – War10ck Commented Oct 22, 2012 at 20:12
Add a ment  | 

5 Answers 5

Reset to default 7

Or maybe you could just write the link as inline js.

​<script>
document.write('<a href="http://www.xyz./Index1/Index2/' + random_generator + '/Index4">hello</a>');
</script>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

I am not sure if i understand your problem. But the following should work if you are trying to dynamically set the link to href (assuming you are not using jquery).

<a href='http://www.xyz./Index1/Index2/Variable/Index4' id="index">Good Morning</a>
<script>
    var newUrl = document.getElementById('index').href;
    newUrl = newUrl.replace("Variable", random_generator);
    document.getElementById('index').href = newUrl;
</script>

You can use jQuery to update the dom element when the script gets executed

First you must give an id to your href

<a href="#" id="myHref"

and then

$('#myHref').attr('href', variable)

Or you can change the link text var varText = "This is a link"; $('#myHref').text(varText);

pretty sure this will wrk

   document.getElementById('myHref').href = variable;

As far as I know this would not be possible. You could do one of two things:

1 Change the HREF attribute when the document loads. With JQuery you could do something like

$(function() {
 $('a').attr('href', 'www.xyz./Index1/Index2/' + random_generator + '/Index4');
});

2 Catch the click event and redirect it with javascript, this would mean even if the user clicked the link multiple times without refreshing the page it would be random.

$(function() {
 $('a').on('click', function(e) {
   e.preventDefault();
   window.location = 'www.xyz./Index1/Index2/' + random_generator + '/Index4';
 });
});

If you don't wish you use jquery I'm sure there's plenty examples online to do this with plain javascript.

发布评论

评论列表(0)

  1. 暂无评论