我编辑了代码,更新后的代码如下,此代码无法获取关键字元标记,因此无效。
旧描述: 我正在尝试连接字符串以获取finalUrl,但由于tags变量,我无法这样做。我需要获取页面的关键字元标记并将其附加到finalUrl。有什么帮助吗?
<script type="text/javascript">
var tags=$('meta[name=keywords]').attr("content");
var gameurl = "http://xyz/abc/details/";
var jsn = ".json?callback=showGameDetail";
var finalUrl= gameurl.concat(tags).concat(jsn);
function loadJSON(url) {
var headID = document.getElementsByTagName("head")[0];
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.src = url;
headID.appendChild(newScript);
}
function showGameDetail(feed){
var title = feed.title;
var game_url = feed.pscomurl;
var packart_url = feed.Packart;
$("#bnr-ads-box").html("<img src='"+"http://abc.com/"+packart_url+"'>");
}
loadJSON(finalUrl);
</script>
<div id="bnr-ads-box"></div>
答案 0 :(得分:1)
<!DOCTYPE html>
<html>
<head>
<meta id="metaK" name="keywords" content="customizable software for QuickBooks, QuickBooks-integrated, Method customization, CRM accounting, Method for QuickBooks, Method CRM, Method blog, Salesforce automation, Method online platform, QuickBooks customization, web-based platform, industry-specific, customer portal, Method Field Services, Method Manufacturing, ERP" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
</head>
<body>
<p id="demo">Click the button to join two strings into one new string.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var tags=$('meta[name=keywords]').attr("content");
var gameurl = "http://xyz/abc/names/";
var jsn = ".json?callback=showGameDetail";
var finalUrl= gameurl.concat(tags).concat(jsn);
document.getElementById("demo").innerHTML=finalUrl;
}
</script>
</body>
</html>
答案 1 :(得分:1)
更改此
var tags="$('meta[name=keywords]').attr("content");";
到
var tags=$('meta[name=keywords]').attr("content");
也使用此代码var finalUrl = gameurl + tags + jsn;
答案 2 :(得分:1)
你需要的是在你的tags变量中转义双引号,如下所示:
var tags="$('meta[name=keywords]').attr(\"content\");";
Cris'解决方案也很好,但在某些情况下,你需要在字符串中有两组双引号,这样你才会被迫正确地进行转义。
FYI:转义是在字符串中生成特殊字符的过程,否则会导致问题,例如在javascript中你不能在字符串中添加换行符,如下所示:
var mystring = 'on
a different line'; // <- this causes a syntax error
所以人们会做以下事情:
var mystring = 'on\na different line';
答案 3 :(得分:1)
很难说,你可以使用一个数组,可以通过调用join()
来连接:
var tags = $('meta[name=keywords]').attr("content");
var data = [
"http://xyz/abc/names/",
encodeURIComponent(tags),
".json?callback=showGameDetail"
].join('');
$("#demo").html(data);
答案 4 :(得分:1)
你忘了包含jquery
<!DOCTYPE html>
<html>
<head>
<meta name="keywords" content="hello"/>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
function myFunction()
{
alert("Hello World!");
var tags=$('meta[name=keywords]').attr("content");
var gameurl = "http://xyz/abc/names/";
var jsn = ".json?callback=showGameDetail";
var finalUrl= gameurl.concat(tags).concat(jsn);
alert(finalUrl);
}
</script>
</head>
<body>
<button onclick="myFunction()">Try it</button>
</body>
</html>
答案 5 :(得分:0)
实际上concat方法也适用于字符串(至少在chrome中),但建议的方法是使用plus concatenation string operator
但你错过了一些东西
<!DOCTYPE html>
<html>
<head>
<title>Create a URL from keywords</title>
<meta name="keywords" content="These are tags" />
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
function myFunction() {
var tags = $('meta[name=keywords]').attr("content");
var URL ="http://xyz/abc/names/" +
encodeURIComponent(tags) +
".json?callback=showGameDetail";
window.console && console.log(URL);
$("#demo").html(URL);
}
</script>
<body>
<p id="demo">Click the button to join two strings into one new string.</p>
<button onclick="myFunction()">Try it</button>
</body>
</html>