开发者

Using javascript to create a value in url and submit that value via form?

开发者 https://www.devze.com 2023-01-30 13:51 出处:网络
I have a site that request that they could send out different urls to clients to track what links are being used. I told them to use google analytics but they are requesting to stay away from it.

I have a site that request that they could send out different urls to clients to track what links are being used. I told them to use google analytics but they are requesting to stay away from it.

What they are asking is they want to send a url to there customers such as, http://www.yoursite.com/?link=Nameoflink

They want to get that cookie and set it. Then when the contact form is used they want to be able to submit that link name with the form submission to show what links are being used to go directly to there site.

I was told this is possible but i have no knowledge of that开发者_运维百科 custom of javascript or cookie expertise... =/


You can get the value of the params passed in through the url with location.search. To get the value of the param, use the location.search and then find the specific url value, then set that in another hidden text field or something...

if (location.search){ 
  var search = location.search.substr(1).split("&"), 
      url = search.split("=")[1];
  document.getElementById('hiddenInput').value = url;
}

Note- the code above assumes that your search string only contains the URL value & that the URL is first. If not, it is likely this will fail. You can update the code to account for that by checking to make sure that search.split("=")[0]==="url" or expanding it to parse out all of the search params into an object that you can reference by key.


Yeah, google analytics would make this a lot easier, especially if you had a specific page that would serve as a drop-point, telling you how many people click this special link.

Without google analytics, you could get the GET variable values via a PHP or ASP page script and have them set that way, or you can use soley JavaScript to take care of cookie setting and retrieval.

For JavaScript, these links should point you in the right direction:

JavaScript cookies: (I can only post one link, but check out W3C School's article on JavaScript cookie handling)

Extract GET values via JavaScript: http://www.go4expert.com/forums/showthread.php?t=2163

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号