简体   繁体   English

使用JavaScript对#,单引号和双引号进行编码

[英]encoding values using javascript for #, single and double quotes

This is the function that I am using to get the value of querystring through javascript. 这是我用来通过javascript获取querystring的值的函数。

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
    results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

I am also calling this function: 我也调用此函数:

function GenerateCode() {
    var trendingitemvalue = getParameterByName('q');
    alert(trendingitemvalue);
    var script = "<script async src=\"//hashgurus.com/feed/Trendingat.js\"><\/script>";
    script += "\n<div class=\"hg-trendingat-widget\" hg-trend=\"" +trendingitemvalue + "\"></div>"

    codearea.value = script;
 }

<textarea style="height: 40px; color: blue" id="codearea" onclick="SelectAll(this)"></textarea>

What am I trying to do? 我想做什么?
I am getting the value of querystring through the function getParameterByName('q') and then framing a script tag (var script) and assigning this value to a textarea field. 我通过函数getParameterByName('q')获取querystring的值,然后构建脚本标记(var脚本)并将此值分配给textarea字段。

for example this is the weburl: http://hashgurus.com/q.aspx?q=%23BDMELODI_161 where (q = #BDMELODI_161) I am getting the value of q(which is the querystring) and framing the script value. 例如,这是weburl: http ://hashgurus.com/q.aspx?q=%23BDMELODI_161其中(q =#BDMELODI_161)我正在获取q(它是查询字符串)的值并将框架值定为框架。 The final value of script looks like this through this code: 通过以下代码,脚本的最终值如下所示:

<div class="hg-trendingat-widget" hg-trend="#BDMELODI_161"></div>

but i need exactly what is in the querystring. 但是我需要在querystring中到底是什么。 The expected result should be like this: 预期结果应如下所示:

<div class="hg-trendingat-widget" hg-trend="%23BDMELODI_161"></div>

Also I need to take care of single and double quotes of the querystring properly. 另外,我需要正确处理querystring的单引号和双引号。 Is there any function in javascript to encode these values? javascript中是否有任何函数可以对这些值进行编码?

If I don't misundersand your aims, using 如果我不误会您的目标,请使用

encodeURIComponent(trendingitemvalue)

should do the work for the hash issue, doesn't it? 应该为哈希问题做好工作,不是吗?

I just tested it and it worked, returning the "%23" replacing the "#". 我刚刚对其进行了测试,然后将其返回“%23”以代替“#”。

If I misunderstood your question, please tell me where I'm wrong and maybe I can help you! 如果我误解了您的问题,请告诉我哪里错了,也许我可以为您提供帮助!

Kind regards. 亲切的问候。

To encode it like you would in an URL you should use encodeURIComponent . 要像在URL中那样对它进行编码,应该使用encodeURIComponent

After that to insert the value securely in HTML attribute you need to HTML encode it, however there are no methos which simply does this in JS, you should or use a "translator" tag or build your div using the native calls: 之后,要在HTML属性中安全地插入值,您需要对其进行HTML编码,但是没有方法可以在JS中简单地做到这一点,您应该或使用“ translator”标签或使用本机调用来构建div:

var div = document.createElement('div');
div.setAttribute('hg-trend', encodeURIComponent(trendingitemvalue));
div.className = "hg-trendingat-widget";
//div.outerHTML will return <div...></div>

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

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