简体   繁体   English

将javascript网址参数传递给html

[英]Pass javascript url parameter to html

I've got a bit of javascript (shown below in a simplified format) which is the "ad tag" from the ad server that brings up an ad unit on the html page. 我有一些javascript(下面以简化格式显示),它是来自广告服务器的“广告代码”,可在html页面上显示广告单元。

<script type="text/javascript" src="http://adserverdomain.net;pcode=1234;city=nameofcity;suburb=nameofsuburb"></script>

The javascript has more variable but I've just shown one. javascript有更多变量,但我只显示了一个。

Below this I have a <div> in which I'd like to pull the variable "pcode" from the above javascript and display it's value using 在此之下,我有一个<div> ,我想从上面的javascript中提取变量“ pcode”,并使用显示它的值

$('div').html("");

So the <div> needs to be populated with the value "1234". 因此<div>需要使用值“ 1234”填充。

Any idea how I can do this? 知道我该怎么做吗? Thanks 谢谢

EDIT: I've updated the url (added .net and some other variables after pcode to avoid confusion). 编辑:我已经更新了网址(在pcode之后添加了.net和其他一些变量以避免混淆)。 Also, I don't have access to the initial script, so I can't add an id to it. 另外,我无权访问初始脚本,因此无法向其添加ID。 The script is generated by the ad server and it always has the variable pcode (with a different value). 该脚本由广告服务器生成,并且始终具有变量pcode(具有不同的值)。 just need to be able to display that in another div on the same html page. 只需能够在同一html页面上的另一个div中显示该内容即可。

Try 尝试

<script type="text/javascript" src="http://adserverdomain;pcode=1234;city=sajdhsk;suburb=asdsadas"></script>
<div id="pcode"></div>
<div id="city"></div>
<div id="suburb"></div>

then 然后

var pcodesrc = $('script[src^="http://adserverdomain;"]').attr('src');

$('#pcode').html(pcodesrc.match(/pcode=(.+?)(?=(;|$))/)[1])
$('#city').html(pcodesrc.match(/city=(.+?)(?=(;|$))/)[1])
$('#suburb').html(pcodesrc.match(/suburb=(.+?)(?=(;|$))/)[1])

Demo: Fiddle 演示: 小提琴

or 要么

$('#pcode').html(pcodesrc.match(/pcode=([^;]+)/)[1])
$('#city').html(pcodesrc.match(/city=([^;]+)/)[1])
$('#suburb').html(pcodesrc.match(/suburb=([^;]+)/)[1])

Demo: Fiddle 演示: 小提琴

Try this with url.Actually the below code get data from url querystring.Edit it and give your url. 尝试使用url。实际上下面的代码从url查询字符串中获取数据,对其进行编辑并提供您的url。

function getUrlVars() {
            var vars = [], hash;
            var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
            for (var i = 0; i < hashes.length; i++) {
                hash = hashes[i].split('=');
                vars.push(hash[0]);
                vars[hash[0]] = hash[1];
            }
            return vars;
        }

var me = getUrlVars()["pcode"];

Try this buddy 试试这个哥们

function getvalues()
    {
        var values = {};
        var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) { values[key] = value;});
        return values;
    }

whenever you want to use it 每当你想使用它

var value = getvalues()["pcode"];

Use this value to put in your html element 使用此值放入您的html元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM