0

I Have edited the code, the updated code is below, This code is not able to fetch the keywords meta tag, hence it is not working.

old description: I am trying to concatinate the strings to get the finalUrl, but I am not able to do so becuase of the tags variable. I need to fetch the keywords meta tag of the page and append it to get the finalUrl. Any help?

  <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>
4
  • 1
    Use a plus: var finalUrl = gameurl + tags + jsn; - It is very googlable using string concatenation to get to MDN Commented Aug 26, 2013 at 8:31
  • If you are trying to evaluate the output of the jquery script you should not put it inside quote. Directly right var tags=$('meta[name=keywords]').attr("content"); Commented Aug 26, 2013 at 8:35
  • I am sure you are missing tags var value here. Always debug whether you are getting right value if you gonna use it later Commented Aug 26, 2013 at 8:35
  • What is the out put of tags if you print it in console. console.debug(tags). Post the output data structure. Commented Aug 26, 2013 at 8:39

6 Answers 6

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Doesn't work when var tags= $('meta[name=keywords]').attr("content"); Works if the value of tags is simple, lets say var tags = "name_of_page"
In my string "tags", let say I am getting following output Keyword1, Keyword2, Keyword3, keyword4 but i need to use on Keyword1 to use in my finalUrl to make the URL, how to achieve it.
1

change this

var tags="$('meta[name=keywords]').attr("content");";

to

var tags=$('meta[name=keywords]').attr("content");

also use this code var finalUrl = gameurl + tags + jsn;

Comments

1

What you need is to escape the double quotes inside your tags variable, like so:

var tags="$('meta[name=keywords]').attr(\"content\");";

Cris' solution is also fine, but in some case you will need to have two sets of double quotes inside a string so you will be forced to do escaping correctly.

FYI: Escaping is the process of having special characters getting generated in a string which would otherwise cause issues, for instance in javascript you can't have newlines in a string, like this:

var mystring = 'on
a different line'; // <- this causes a syntax error

So one would do the following:

var mystring = 'on\na different line';

3 Comments

See my answer which is using encodeURIComponent to avoid that
Actually, @mplungjan covered more stuff than mine - but it lacks some explaining. :)
Updated while you commented :)
1

You forgot to include the 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>

Comments

1

Tough debatable, you can use an array, which can be concatenated by calling join():

var tags = $('meta[name=keywords]').attr("content");
var data = [
 "http://xyz/abc/names/",
 encodeURIComponent(tags),
 ".json?callback=showGameDetail"
].join('');
$("#demo").html(data);

4 Comments

Euuw. At least push the strings if you want to use an array
This is a very elegant solution than having all the stuff on one line. A different and elegant way would be to have the different strings in properly named variables: var url = "http://xyz/abc/names/", query = ".json?callback=showGameDetail"; var data = url + encodeURIComponent(tags) + query;
I do not see the elegance over having + in this particular example - it adds unnecessary [].join("")
No need to use push() as an array can be initialized directly. Besides it would be overkill to use push() if the main goal is to produce a string.
0

Actually the concat method works on strings too (in chrome at least) but the recommended method is using the plus concatenation string operator

You are however missing some stuff

  1. jQuery library - I assume you want that since you have $(...) in the example
  2. encoding of the string from the keywords - I use encodeURIComponent to handle possible newlines and quotes in the keywords

.

<!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>

2 Comments

The question was misusing (actually, missing) the concept of string-escaping. I would cover this as well in my answer (as I already did) - at least for the sake of learning.
Is someone on a downvoting spree here? Why is this downvoted without comment?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.