Implement s2s pixel - javascript

I have to implement s2s pixel in website and my pixel is as
.
I have to implement it using javascript and they send me also javascript code as
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, " "));
}
</script>
In my landing page there is download button when a user clicks on that pixel should be fired plz help me in this

Related

Javascript regex to get param value

I'm working on a code base someone else built and the search function uses this to get the parameter from the url to place as a value to search against. I've tried re-writing it, modifiying it, and everything else possible and can't figure out what is going on with the reutrn value from the function.
Here's the function
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
console.log(name);
var regex = new RegExp("[\\?&#;]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
console.log(results);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var part = getParameterByName('part_name');
The value of location.search is ?part_number=1234&go=GO
Then in the input box part_name if I type 1234 to be the value of the passed in parameter it will only show 12. However typing in other random numbers it will sometimes show them all correctly. If I type 124 as the search param then it displays correctly as searching against 124.
Is there something wrong with one of these regex statments that could be causing this, or is this something on the server side? The PHP seems fine on the backend but I'm not savvy on the Regex used in the Javascript code.
EDIT:
A testing snippet:
function getParameterByName(loc, name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
document.getElementById("res").innerHTML += "Name: " + name + " - ";
var regex = new RegExp("[\\?&#;]" + name + "=([^&#]*)"),
results = regex.exec(loc);
document.getElementById("res").innerHTML += results + "<br/>";
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var part = getParameterByName('?part_number=1234&go=GO', 'part_number');
var part2 = getParameterByName('?part_name=12&go=GO', 'part_name');
<div id="res"/>

Get values of all the same parameter from url in jQuery

I have an url have contain two parameters.
Ex: https://www.google.com.vn/search?q=news&oq=news&aqs=chrome..69i57j69i60l3.299j0j4&sourceid=chrome&es_sm=93&ie=UTF-8#q=bbc
Parameter 1: q=news
Parameter 2:q=bbc
I want to get all value have the same parameter, but I can only get value in first the parameter.
This is my code:
function getParameterByName(name, url) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&#]" + name + "=([^&#]*)"),
results = regex.exec(url);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
Use $(location).attr('href'); and parse through the parameters yourself
what i have tried is only IDEA of how you can get all paremater.
var a="https://www.google.com.vn/search?q=news&oq=news&aqs=chrome..69i57j69i60l3.299j0j4&sourceid=chrome&es_sm=93&ie=UTF-8#q=bbc"
var b= a.split("?")[1]
b.split("&")
By this technique you will get all parameter.
You need to convert this code to your required code.
The first q is in querystring, the second one is in hash. You should parse them in JavaScript:
QueryString
// the method is taken from http://stackoverflow.com/a/3855394/3971911
var qs = (function(a) {
if (a == "") return {};
var b = {};
for (var i = 0; i < a.length; ++i)
{
var p=a[i].split('=');
if (p.length != 2) continue;
b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
}
return b;
})(window.location.search.substr(1).split('&'));
Hash
// the method is taken from http://stackoverflow.com/a/11920807/3971911
function getHashValue(key) {
return location.hash.match(new RegExp(key+'=([^&]*)'))[1];
}
Now you can use them like this:
alert(qs['q']); // news
alert(getHashValue('q')); // bbc
try this:
function getParameterByName(name, url,position) {
url=position==1?url:url.substr(url.lastIndexOf('#'));
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&#]" + name + "=([^&#]*)"),
results = regex.exec(url);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var url='https://www.google.com.vn/search?q=news&oq=news&aqs=chrome..69i57j69i60l3.299j0j4&sourceid=chrome&es_sm=93&ie=UTF-8#q=bbc';
alert(getParameterByName('q',url,1));
alert(getParameterByName('q',url,2));
Demo
Other than an exercise in coding, you're probably better off using existing code.
Something like:
https://github.com/sindresorhus/query-string
Pros:
It has tests
It's had more useful eyes cast over it (guessing here).
The people that have likely contributed to it will probably have used it.
If you find an issue with it, those people involved will have a keen interest in solving the problem as they're likely using it.
Cons:
None! :D

get URL parameter values using Javascript

I have created an application for getting the url parameter values using JavaScript, the application is working fine but the problem is that when the param key is having space then i am getting undefined
my code is as given below
script
function getParameter(paramName )
{
paramName = paramName.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + paramName + "=([^&#]*)", 'i'), results = regex.exec(location.search);
return results == null ? undefined : decodeURIComponent(results[1].replace(/\+/g, " "));
}
let say the url is like
http://localhost/myapp/index.html?User Id=145&Dept=HR
call
getParameter('Dept') ===> Gives HR
getParameter('User Id') ===> Gives undefined
can anyone please tell me some solution for this

how to get the parameter in html

I create the html for Label & text box and OK button.
I just pass parameter from the javascript like that
../test/Html/MWD.Numeric.html?id=1.0000
I would like to set that id value in my text box #html.
How to set the id in text box. When I updated that id in html, I would like to update in parent screen.
How to do? Please kindly guide me. Thank in advance.
duplicate of How can I get query string values in JavaScript?
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, " ")); }

What should I write in my jQuery function to get the value from my inputfield (id=btnCloseDeal) and display it on another page?

When enter/writing a name in a inputfield (id=btnCloseDeal), and after that I click my GoToPayBtn Im coming to a new page. On my new page I wanna get the value from my inputfield (id=btnCloseDeal) and display it. What should I write in my jQuery function to get the value from my inputfield (id=btnCloseDeal) and display it on another page?
<input type="button" class="artikel" id="btnCloseDeal" value="Pay" onclick="check();";"/>
$("#btnCloseDeal ").click(function (){}
As I understand your question, the way you are thinking of implementing it is not optimal.
I would suggest using your field (#btnCloseDeal) as a query parameter and then fetching it with jQuery.
Here is a function that will get you a query-param in pure JS
function getParameterByName(name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.search);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
you can set the value of the input to the url of a page you want to redirect to, then get the url parameter with javascript on that page:
// get the url
var url = window.location.protocol + "//" + window.location.host;
and use:
var param = location.search;

Categories

Resources