0

Today, I come to ask for your help on a small javascript problem. how can url work with this jquery

example url

http://mywebsite.com/?link=f9891188&title=My_Post_Title&dLink1=http://url.com/one&dLink2=http://url.com/two&dLink3=http://url.com/three&dLink4=http://url.com/four&gplay=1234

this html and javascript

function GetUrlValue(a) {
 var b = window.location.search.substring(1);
 var c = b.split('&');
 for (var i = 0; i < c.length; i++) {
  var d = c[i].split('=');
  if (d[0] == a) {
   return d[1]
  }
 }
}
$('#title').html('<h2>' + GetUrlValue('title') + '</h2>');
if (geoip_country_code() == 'TK' || geoip_country_name() == 'Tokelau') {}
else {
 if (GetUrlValue('dLink1') == '') {
  $('#url1').html('No link')
 } else {
  $('#url1').attr('onclick', "this.href='" + GetUrlValue('dLink1') + "'")
 }
 if (GetUrlValue('dLink2') == '') {
  $('#url2').html('No link')
 } else {
  $('#url2').attr('onclick', "this.href='" + GetUrlValue('dLink2') + "'")
 }
 if (GetUrlValue('dLink3') == '') {
  $('#url3').html('No link')
 } else {
  $('#url3').attr('onclick', "this.href='" + GetUrlValue('dLink3') + "'")
 }
 if (GetUrlValue('dLink4') == '') {
  $('#url4').html('No link')
 } else {
  $('#url4').attr('onclick', "this.href='" + GetUrlValue('dLink4') + "'")
 }
 $('#gp').attr('onclick', "this.href='https://play.google.com/store/apps/details?id=" + GetUrlValue('gplay') + "'");
}
$(document).ready(function () {
 document.title = ('' + GetUrlValue('title') + '');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script src="http://j.maxmind.com/app/country.js" charset="ISO-8859-1"></script>

<section>
<div id='container_buttons'>
<p>
<a class='a_btn 1' href='#' id='url1' onclick='' rel='nofollow' target='_blank' title='Download'>Download 1</a>
</p>
<p>
<a class='a_btn 2' href='#' id='url2' onclick='' rel='nofollow' target='_blank' title='Download'>Download 2</a>
</p>
<p>
<a class='a_btn 3' href='#' id='url3' onclick='' rel='nofollow' target='_blank' title='Download'>Download 3</a>
</p>
<p>
<a class='a_btn 4' href='#' id='url4' onclick='' rel='nofollow' target='_blank' title='Download'>Download 4</a>
</p>
</div>
</section>

1 Answers1

0

I'm guessing a bit at what you want, but if you are trying to read values out of the query string and set the anchor tags href attribute accordingly, you could proceed as follows.

Here is a good function to extract values from a query string:

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, " "));
}

Ref: How can I get query string values in JavaScript?

With the rest, you could DRY up your if/else statements a little by selecting any a elements whose id begin with url, then setting the href accordingly:

$("a[id^='url']").each(function(){
  var linkParam = "dLink" + this.id.replace("url", "");

  if (getParameterByName(linkParam)) {
    this.href= getParameterByName(linkParam);
  } else {
    $(this).text('No link')
  }
});

For the following query string (notice dLink4 is missing):

http://example.com/?link=f9891188&title=My_Post_Title&dLink1=http://url.com/one&dLink2=http://url.com/two&dLink3=http://url.com/three

You get the following markup:

<section>
  <div id="container_buttons">
    <p>
      <a class="a_btn 1" href="http://url.com/one" id="url1">Download 1</a>
    </p>
    <p>
      <a class="a_btn 2" href="http://url.com/two" id="url2">Download 2</a>
    </p>
    <p>
      <a class="a_btn 3" href="http://url.com/three" id="url3">Download 3</a>
    </p>
    <p>
      <a class="a_btn 4" href="#" id="url4">No link</a>
    </p>
  </div>
</section>
Community
  • 1
  • 1
James Hibbard
  • 16,490
  • 14
  • 62
  • 74