Justin Erswell
Justin Erswell

Reputation: 708

JQuery Change HREF by part of a URL

I have a url on my page which has a dynamic element...

<a class="TOPHEADING" href="/CRM/eware.dll/SessionFind?&Act=200&CLk=T&Key0=1&Key1=2" target="EWARE_MID">

Namely the key1 and key0 values I would like to prate the href with the following code...

var strPath = document.URL;
var arrayApp = strPath.split("&Act");
var strStartPath = arrayApp[0]+GetKeys()+"&Act=432"+"&dotnetdll=Customs&dotnetfunc=RunCompanySummary";
document.location.href= strStartPath;

the new link will be held in the strStartPath variable.

So essentially I need to replace the deployed href with that of the variable strStartPath on the load of the page.

Is this possible in JQuery and if so how?

Upvotes: 0

Views: 301

Answers (1)

Ofir Baruch
Ofir Baruch

Reputation: 10356

It "seems very complex"? It's really easy , let's break this code:

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

This function gets one parameter "name". The first 2 lines are the regex expression, the third one defines a new regex object. The 4th line excute the regex exp. on the "window.location.hre" which is the URL of the current page , the condition checks if there's a result of the expression in the current url or not.

Basicly , for your needs:

<a href="http:/www.bla.com/g.php?key0=11212&key1=2222&bla=wewe" id="link1">

<script>
var link = document.getElementById("link1");
var url  = link.href;
var keyZeroValue = getParametersByname("key0" , url);
var keyOneValue = getParametersByname("key1" , url);

function getParameterByName(name,url)
{

  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(url);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

this should do the work.

Upvotes: 2

Related Questions