I've found several posts alluding to the location.href method. But I can't find anything about how to use the variable on the next page that's opened.
I have a function with a single line of code in an external js file:
function nextPage()
{location.href='page2.html?foo=' + src;}
It's activated by a button in the html file. How do I use this on the next page that's opened? I'm assuming this makes 'foo' available. ('src' is an integer stored as a global variable in the external js file. It's just a number between 1 and 5).
On the next page you can get the value using:
http://page2.html?foo=
location.search
> ?foo=
location.search.substring(1)
> foo=
In Script Tag You Put
var array=location.search;
var data = array.split("foo=");
var divid=data[1];//It has your foo value
if you are using javascript then try this code to get querystring value
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, " "));
}
var value = getParameterByName('foo');
Related
I am working with the below Javascript function. It only works when the value is a number. I mean it only returns the value if it is a a number. For example:
var ldInstID = getParameterByName("ID")
If ID is a number then it works and assigns the value to the variable but if the ID is a string it is not working. Please help to make this work for a string too.
I am using this on SharePoint list edit page where ID is a list column value. I want to capture another column city and pass it as href query string along with ID.
In the attached images you can see that ldInstID is blank
<!--
Name: dispParent.js
-->
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
//get the ID for the Issue from the Query String
// var issueID = getParameterByName("ID");
var ldInstID = getParameterByName("LeadInsitution");
//find the element with the "Add new item" link.
//note that if you have more than one list on your page, this just finds the first one
var anchorElement = $("a[title='Add a new item to this list or library.']");
//modify the "Add new item" link to call the "NewItem2" function and pass in the Issue ID.
//Be sure to put the path to your site below. You can use relative URL to the web application or the FQDN
// $(anchorElement).attr("href","javascript:NewItem2(event,'URL/Lists/Time/NewForm.aspx?IssueID=" + issueID + "');");
// $(anchorElement).attr("href","javascript:NewItem2(event,'URL/NewForm.aspx?IssueID=" + issueID + "&LdInst" + LdInst + "');");
$(anchorElement).attr("href","javascript:NewItem2(event,'URL/NewForm.aspx?LdInstID=" + ldInstID + "');");
//remove the "onclick" attribute from the anchor element as we aren't using it anymore
$(anchorElement).removeAttr("onclick");
});
// no, I didn't write this function from scratch, I found it at
// http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript
// http://www.sharepointhillbilly.com/Lists/Posts/Post.aspx?ID=26
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, " "));
}
</script>
variable passed-ID
Variable passed- ldInstID
Get parameter by id from URL:
function getURLParameter(parameterName) {
let result = null, temp = [];
location.search
.substr(1)
.split('&')
.forEach(function (item) {
temp = item.split('=');
if (temp[0] === parameterName)
result = decodeURIComponent(temp[1]);
});
return result;
}
if my url is http://example.com?id1=100&text=my%20text
console.log(getURLParameter('id1')); // 100
console.log(getURLParameter('text')); // "my text"
I have been collecting UTM parameters and using the values to populate form fields successfully for some time. Instead of defaulting to a blank or null value if my utm_source is blank, I would like to provide a default value for utm_source of "website"
Here's my code:
// Parse the URL
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)");
var results = regex.exec(location.search);
return results === null
? ""
: decodeURIComponent(results[1].replace(/\+/g, " "));
}
// Give the URL parameters variable names
var source = getParameterByName("utm_source");
var medium = getParameterByName("utm_medium");
var campaign = getParameterByName("utm_campaign");
// Put the variable names into the hidden fields in the form.
// selector should be "p.YOURFIELDNAME input"
document.querySelector("p.utm_source input").value = source;
document.querySelector("p.utm_medium input").value = medium;
document.querySelector("p.utm_campaign input").value = campaign;
No error messages and the script works as intended. I am simply looking to add a default value in the event one of the parameters is blank. How can I do that?
you should be able to do it just via conditional assuming your getParameterByName function returns undefined or a similar falsy value when there is nothing found:
var source = getParameterByName('utm_source') || 'my default value';
I am working in Pardot, if it matters, but have some javascript which will write GA utm parameters to hidden fields.
Unfortunately, if any of the values are blank, it will pass that blank value, and overwrite any existing value. It's important that I am able to overwrite existing values, but only when there is data in the query string.
Is it possible to write default values instead? So for example, a form is submitted with no utm_source, then we write Source="Organic", or something similar.
Here is my code:
<script type="text/javascript">
// Parse the URL
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, " "));
}
// Give the URL parameters variable names
var source = getParameterByName('utm_source');
var medium = getParameterByName('utm_medium');
var campaign = getParameterByName('utm_campaign');
var campaign = getParameterByName('utm_content');
// Put the variable names into the hidden fields in the form. selector should be "p.YOURFIELDNAME input"
document.querySelector("p.source input").value = source;
document.querySelector("p.utm_medium input").value = medium;
document.querySelector("p.utm_campaign input").value = campaign;
document.querySelector("p.utm_content input").value = content;
</script>
Thank you so much for your insight!
Since you make the value "" if empty, and since that is falsey in javascript, just put an if statement before each of the 4 value statements:
if (source) document.querySelector("p.source input").value = source;
In long form, you are saying that if the value exists, then fill in the element with the value. Otherwise, don't do anything:
if (source) {
document.querySelector("p.source input").value = source;
}
I am loading a form in an iFrame using javascript and adding the referrer into URL to pass the parameter to the iFrame (step 1) for populating a hidden field on a form (step 2).
Step 1 works fine and yields something similar to this:
http://www.parentdomain.com/parentpage/?refURL=http://www.somereferringURL.com/someRefpage/
Step 2 (where I am getting stuck)
function gup( grabREF )
{
grabREF = grabREF.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+grabREF+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
var referrer = gup( 'refURL' );
function start() {
var ref = document.getElementById('my-formfield-id');{
ref.value = referrer;
}
onload = start;
}
This only (still) seems to be yielding the parent as the referrer (http://www.parentdomain.com/parentpage/), and (in fact) seems to be appending the URL string with a closing tag. The form and the parent reisde in different sub-domains. I am guessing the culprit may lye here: ref.value = referrer;
Any ideas?
I was unable to get this to work so I resorted to grabbing only the iframe parent as the referrer and changing the parent page (and embedding the same form in the iframe) to capture the different campaign referr URLs I needed. I can still grab them in one location as I am using the same form, it just would have been nice to have just one parent (landing page) and pass along referrer to the parent.
This code will do that:
<script type="text/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, " "));
}
var refVAL = getParameterByName('refURL');
//document.write(refVAL);
function SetValue()
{
document.getElementById('my_hidden_formfield_id').value = refVAL;
//alert(document.getElementById('my_hidden_formfield_id').value);
}
</script>
Thanks for looking.
So I have this quiz with two separate start files. Then I have javascript that takes the url argument ?type=ohs and ?type=dls and display certain images depending on the argument. I then have a complete page. People with the ?type=dls argument get a link that takes them somewhere else, and people with ohs get just some confirmation text. I want it to be contained on the same complete page, but I am having a hard time trying to figure it out. The javascript I have is:
function go_to_url(page_name) {
location.href = page_name + "?" + location.href.split('?')[1];
}
That is to display images depending on the url argument. How do I create the javascript for the complete page that will display certain text or a link depending on the url arguments ?type=ohs and ?type=dls?
It sounds like you want to get values from the query string
here is a function to do that:
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, " "));
}
Taken from the answer to this question
How can I get query string values in JavaScript?
var v = getParameterName("type"); //Will return ohs or dls
if(v == "ohs")
//display text for ohs
else
//display text for dls