This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Pass vars to JavaScript via the SRC attribute
May I know how to read get the p value on js file link like filename.js?p=value with local javascript in the js file? Any function to work like the $_GET['p'] in php? Thanks.
try this:
var tmp = location.href, it;
if (q) it = tmp.substr(q + 1).split('&');
else it = '';
for (var i in it) {
var t = it[i].split('=');
if (t[0] == 'p') {
//do something
break;
}
}
function _GET( 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 results[1];
}
This will do the equivalent in javascript.
Related
So I have embedded iFrame forms on my site and need to implement persistent tracking for users, meaning that their UTM parameters pass between pages and then get submitted into hidden fields with the form.
I believe the best way to do this is by setting a cookie to capture the UTM params on the parent site (done), and then reading the cookie to set a query string on the iFrame src. From there, I can pass the hidden values from the iFrame src (that's also already done).
What I'm having trouble figuring out is how to connect the dots. and have searched around quite a bit without finding a clear answer. And I'm showing my work but certainly open to other suggestions.
So I have these code snippets...
First, just above the iFrame embed, there's this:
const getCookie = (__gtm_campaign_url) =>{
// Construct a RegExp object as to include the variable name
const re = new RegExp(`(?<=${cookie_name}=)[^;]*`);
try{
return document.cookie.match(re)[0]; // Will raise TypeError if cookie is not found
}catch{
return "this-cookie-doesn't-exist";
}
}
getCookie('__gtm_campaign_url') //
getCookie('_non_existent') // this-cookie-doesn't-exist
Then, to generate the iFrame embed:
var form = 'https://go.mysite.com/some-unique-ids';
var params = window.location.search; // I know this needs to change, just don't know how
var thisScript = document.scripts[document.scripts.length - 1];
var iframe = document.createElement('iframe');
iframe.setAttribute('src', form + params);
iframe.setAttribute('width', '100%');
iframe.setAttribute('height', 500);
iframe.setAttribute('type', 'text/html');
iframe.setAttribute('frameborder', 0);
iframe.setAttribute('allowTransparency', 'true');
iframe.style.border = '0';
thisScript.parentElement.replaceChild(iframe, thisScript);
Finally, within the iFrame, there's this snippet to parse the cookie and get it into the proper hidden fields. Frankly, this is leftover from something else I've tried but didn't work, so it could/should be eliminated if I could get the cookie parsed into var params = [foo] above, but including it for reference:
// Parse the Cookie
function getCookie('__gtm_campaign_url') {
var name = __gtm_campaign_url + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
// Parse the URL inside Cookie
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)");
results = regex.exec(getCookie("__gtm_campaign_url"));
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
// Pass the values to hidden field
document.querySelector("input#input_3_9").value = getParameterByName('utm_source');
document.querySelector("input#input_3_9").value = getParameterByName('utm_term');
document.querySelector("input#836443_81170pi_836443_81170").value = getParameterByName('utm_medium');
document.querySelector("input#input_3_8").value = getParameterByName('utm_campaign');
document.querySelector("input#input_3_11").value = getParameterByName('utm_content');
This question already has answers here:
Check if a JavaScript string is a URL
(36 answers)
Closed 9 years ago.
I am writing a function to check if the input string is a url in Javascript. Should I use substring(0,6) and see if starts with "http://"? Or there is a better way to achieve? Cheers.
Something like this should handle the simple cases:
function is_url(url) {
return Boolean(url.match(/^https?:\/\//));
}
You could use a regular expression
/^http:\/\//.test(urlString)
You could use:
if(myvalue.indexOf('https://') == 0 || myvalue.indexOf('http://') == 0)
Depends how detailed you want to get with it. I am sure you can find a regex that would do it on here is you searched around.
With regex:
/^http:/.test("http://example.com/")
If you wanted to check www too: /^(http:|www\.)/.test("http://example.com/")
And to be different:
function matchString(str,matches)
{
if(matches)
{
matchString.toCheck=matches;
}
var matched = [];
for(var i=[0,str.length];i[0]<i[1]; i[0]++)
{
for(var j=[0,matchString.toCheck.length];j[0]<j[1]; j[0]++)
{
if(!matched[j[0]])matched[j[0]]={c:0,i:-1};
if(matchString.toCheck[j[0]][matched[j[0]].c]==str[i[0]])
{
matched[j[0]].c++;
if(matched[j[0]].i==-1)matched[j[0]].i=i[0];
}
else if(matchString.toCheck[j[0]].length!=matched[j[0]].c)matched[j[0]]={c:0,i:-1};
}
}
return matched;
}
var urlVariants = matchString("https://",["http://","https://","www."]);
var isUrl = false;
for(var i=[0,urlVariants.length]; i[0]<i[1]&&!isUrl; i[0]++)
{
isUrl = (urlVariants[i[0]].i==0);//index at the start
}
console.log(isUrl);
I think regex is a better solution:
function isAnUrl(url){
var expression = /[-a-zA-Z0-9#:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9#:%_\+.~#?&//=]*)?/gi;
var regex = new RegExp(expression);
if (url.match(regex))
return true;
else return false;
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Remove characters from a string
I have a variable p which I would like to remove the $ from. This variable will be a number such as $10.56. How can I do this? I thought it could be done using .replace('$','') but i'm not quite sure how to implement this.
Here is my javascript code:
function myFunction() {
var p = parseFloat(document.getElementById('p_input').value);
var q = parseFloat(document.getElementById('q_input').value);
if (!q){
document.getElementById('t').value = '';
}
else {
var t = q * p;
document.getElementById('t_output').value = t;
}
}
It's pretty simple:
var myString = "$15.62"
console.log(myString.replace('$', ''));
//Logs: "15.62"
Please note that this new value is not actually "saved" to myString, you'll have to assign it to a variable, yourself:
var newString = myString.replace('$', '');
Try this, assuming that the values of p_input and q_input will be the money values:
var p = parseFloat(document.getElementById('p_input').value.replace('$', ''));
var q = parseFloat(document.getElementById('q_input').value.replace('$', ''));
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to get GET and POST variables with JQuery?
Get query string values in JavaScript
lets say my site has url http://www.akbrowser.tk/ds/?q=http://www.chess.com&r=http://www.blackle.com
(the two parameters are URLs)
I now want to get two javascript variables on the site, with the values of the two urls. (so the first variable would be the chess.com, and the second would be blackle.com [of course it would have the http and all, but I can only post one hyperlink])
how would I do that?
I saw some other similar questions on this site, and the poster gave a long solution that I didn't understand (I think it had something to do with find a '=' and take everything after it) but in this case it would give "http:// www.chess.com&r=http://www.blackle.com [without the space]" as one of the variables.
I also saw another post with multiple parameters like mine, but the poster gave a long solution so since I didn't understand it, I couldn't really make it do what I wanted it to do.
so can someone help me?
Try this function:
function getQueryParam(href, paramName) {
var query = href.substring(href.indexOf('?')+1);
var params = query.split('&');
for(var i = 0; i < params.length; i++) {
var param = params[i].split('=');
if(param.length > 1) {
if(param[0] == paramName) {
return param[1];
}
}
}
return null;
}
console.log(getQueryParam('http://www.akbrowser.tk/ds/?q=http://www.chess.com&r=http://www.blackle.com', 'r'));
function gup(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 results[1];
}
var qString = gup("q");
var rString = gup("r");
What this does is do a regex to find whatever [name] you pass in to the function.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Get query string values in JavaScript
What is the best way to get "test1" from
http://localhost:3311/blabl/allprofiles.aspx?username=test1
, and through PageMethod pass it to webmethod. I think one way is to take from window.location.pathname, cut the string and pass it like a parameter.
May be you can use only javascript like:
var search = function(){
var s = window.location.search.substr(1),
p = s.split(/\&/),
l = p.length,
kv, r = {};
if(l === 0){return false;}
while(l--){
kv = p[l].split(/\=/);
r[kv[0]] = kv[1] || true;
}
return r;
}();
Then use in your code search.username
Try
string username = Request.QueryString["username"];
In a PageMethod, you can do
string username = HttpContext.Current.Request.QueryString["username"];