Declaring JavaScript variables in a HTML link - javascript

Say I have a simple HTML document with the following:
cake
pie
and results.html contains a variable that needs to be declared in Javscript as 100 for the first link, 200 for the second, and so on.
Is there any way I can make it so that when you click on the first link, it opens results.html with a javascript variable set to cake, the second one opens results.html with the variable set to pie, and so on?

You can pass them in the query string. For example:
cake
pie
In your results.html page you get and parse the query string. For example:
var queryStr = window.location.search.substring(1);
var params = parseQueryStr(queryStr);
console.log(params["result"]);
var parseQueryStr = function(queryStr) {
var params = {};
var queryArray = queryStr.split("&");
for (var i = 0, var l = queryArray.length; i < l; i++ ) {
var temp = queryArray[i].split('=');
params[temp[0]] = temp[1];
}
return params;
};

cake
pie
In results.html :
<script type="text/javascript">
function getQueryParams(qs) {
qs = qs.split("+").join(" ");
var params = {}, tokens,
re = /[?&]?([^=]+)=([^&]*)/g;
while (tokens = re.exec(qs)) {
params[decodeURIComponent(tokens[1])]
= decodeURIComponent(tokens[2]);
}
return params;
}
var query = getQueryParams(document.location.search);
alert(query.keyword);
</script>

Related

JS Function URL Parameters - Null Value [duplicate]

This question already has answers here:
Get the values from the "GET" parameters (JavaScript) [duplicate]
(63 answers)
Closed 2 years ago.
I am trying to pull URL parameters to pass to a prebuilt HTML form.
The example URL is:
?state=CA&**period**=PERIOD_FIXED_30YEARS&**loan**=200000&ltv=80&**transaction**=54&property_type=34&fico=740&occupancy=49&cashout=0&rate=4.125&fees=510&points=0.204&trackingID=1445830871741638005
The text in bold is what I would need to pull.
I was trying to do this through:
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, " "));
}
//Gathering the URL Parameters
var period = getParameterByName('period');
var loan = getParameterByName('loan');
var ltv = getParameterByName('ltv');
var transaction = getParameterByName('transaction');
var property_type = getParameterByName('property_type');
var fico = getParameterByName('fico');
var occupancy = getParameterByName('occupancy');
var cashout = getParameterByName('cashout');
var rate = getParameterByName('rate');
var fees = getParameterByName('fees');
var points = getParameterByName('points');
var trackingID = getParameterByName('trackingID');
And then assign to the form using
document.getElementById("tfa_49").value = period;
document.getElementById("tfa_50").value = loan;
I keep getting a null error and I think it is the regex.
Any help would be appreciated. I searched similar questions on the site and none of the answers worked in my situation.
UPDATING WITH WORKING CODE:
<script type="text/javascript">
//Used to pull URL
const link = window.location.href;
const url = new URL(link);
//Function to get the URL parameters
function getParameterByName(name) {
return url.searchParams.get(name);
}
//Gathering the URL Parameters and writing to console to check if parse is accurate -- Note the console.log can be removed if needed
var period = getParameterByName('period');
console.log(period);
var loan = getParameterByName('loan');
console.log(loan);
var ltv = getParameterByName('ltv');
console.log(ltv);
var transaction = getParameterByName('transaction');
console.log(transaction);
var property_type = getParameterByName('property_type');
console.log(property_type);
var fico = getParameterByName('fico');
console.log(fico);
var occupancy = getParameterByName('occupancy');
console.log(occupancy);
var cashout = getParameterByName('cashout');
console.log(cashout);
var rate = getParameterByName('rate');
console.log(rate);
var fees = getParameterByName('fees');
console.log(fees);
var points = getParameterByName('points');
console.log(points);
var trackingID = getParameterByName('trackingID');
console.log(trackingID);
//On load function to prefill the forms hidden fields
$( document ).ready(function() {
document.getElementById("tfa_49").value = period;
document.getElementById("tfa_50").value = loan;
document.getElementById("tfa_51").value = ltv;
document.getElementById("tfa_53").value = transaction;
document.getElementById("tfa_54").value = property_type;
document.getElementById("tfa_55").value = fico;
document.getElementById("tfa_56").value = occupancy;
document.getElementById("tfa_57").value = cashout;
document.getElementById("tfa_58").value = rate;
document.getElementById("tfa_59").value = fees;
document.getElementById("tfa_60").value = points;
document.getElementById("tfa_61").value = trackingID;
});
</script>
You can create an URL object and then use the url.searchParams.get(param) method to get the desired parameter. More information.
Working example:
const link = "http://www.example.com/test.html?state=CA&period=PERIOD_FIXED_30YEARS&loan=200000&ltv=80&transaction=54&property_type=34&fico=740&occupancy=49&cashout=0&rate=4.125&fees=510&points=0.204&trackingID=1445830871741638005";
const url = new URL(link);
function getParameterByName(name) {
return url.searchParams.get(name);
}
//Gathering the URL Parameters
var period = getParameterByName('period');
console.log(period);
The hardcoded url is just for the example with your URL, in your code use:
const url = window.location.href

How to split Javascript code (bs4) using Python

So I have been having some issues when trying to scrape Javascript values from a bs4 code.
Basically the javascript looks like
<script type="text/javascript">
var FancyboxI18nClose = 'Close';
var FancyboxI18nNext = 'Next';
var FancyboxI18nPrev = 'Previous';
var PS_CATALOG_MODE = false;
var ajaxsearch = true;
var attribute_anchor_separator = '-';
var blocksearch_type = 'top';
var combinationsFromController = {"163972":{"attributes_values":{"15":"40"},"attributes":[75],"price":0,"specific_price":false,"ecotax":0,"weight":0.6,"quantity":1,"reference":"IDP20059--IDPA163972","unit_impact":0,"minimal_quantity":"1","date_formatted":"","available_date":"","id_image":-1,"list":"'75'"}};
var comparator_max_item = 0;
</script>
and what I am trying to do here is to scrape the value var combinationsFromController = however what I tried to do is:
bs4 = soup(requests.text, 'html.parser')
for nosto_sku_tag in bs4.find_all('script', {'type': 'text/javascript'}):
if 'combinationsFromController' in nosto_sku_tag.text.strip():
print(nosto_sku_tag)
for att, values in json.loads(
re.findall('var combinationsFromController = (\{.*}?);', nosto_sku_tag.text.strip())[0][:-1]).values():
print(values)
Which gives me an error of Expecting ',' delimiter: line 1 column 4112 (char 4111)
I did realized that whenever I try to do
for nosto_sku_tag in bs4.find_all('script', {'type': 'text/javascript'}):
if 'combinationsFromController' in nosto_sku_tag.text.strip():
print(nosto_sku_tag)
print("---------")
The outprint gives me:
var FancyboxI18nClose = 'Close';
var FancyboxI18nNext = 'Next';
var FancyboxI18nPrev = 'Previous';
var PS_CATALOG_MODE = false;
var ajaxsearch = true;
var attribute_anchor_separator = '-';
var blocksearch_type = 'top';
var combinationsFromController = {"163972":{"attributes_values":{"15":"40"},"attributes":[75],"price":0,"specific_price":false,"ecotax":0,"weight":0.6,"quantity":1,"reference":"IDP20059--IDPA163972","unit_impact":0,"minimal_quantity":"1","date_formatted":"","available_date":"","id_image":-1,"list":"'75'"}};
var comparator_max_item = 0;
----------------------------
Which seems to mean that the javascript code is as one code which I believe maybe needs to split, However I tried to use regex for it but it didn't help me.
So my question is how am I able to scrape ONLY the value var combinationsFromController =?
Use the following regex pattern to isolate the entire javascript object which is assigned to that variable.
combinationsFromController = (.*?);
Try it here.
E.g.
import requests, re, json
r = requests.get(url)
p = re.compile(r'combinationsFromController = (.*?);', re.DOTALL)
data = json.loads(p.findall(r.text)[0])

Extract substring of e-mail message body to google spreadsheet

I'm using google app scripts to extract e-mail data into a google spreadsheet. I've got the below working code that I am trying to modify. I'm sure there's a smarter way ... but for now this works
function emf() {
var ss = SpreadsheetApp.getActiveSheet();
var label = GmailApp.getUserLabelByName("tkh_emf");
var threads = label.getThreads();
for (var i=0; i<threads.length; i++)
{
var messages = threads[i].getMessages();
for (var j=0; j<messages.length; j++)
{
var name = messages[j].getPlainBody().split("Name*:")[1].split("\n")[0];
var email = messages[j].getPlainBody().split("E-mail*:")[1].split("\n")[0];
var phone = messages[j].getPlainBody().split("Phone:")[1].split("\n")[0];
var addr = messages[j].getPlainBody().split("Street Address:")[1].split("\n")[0];
var city = messages[j].getPlainBody().split("City*:")[1].split("\n")[0];
var find = messages[j].getPlainBody().split("hear about us?*:")[1].split("\n")[0];
var sub = messages[j].getSubject().split("Feedback via the ")[1].split("[")[0];
var num = messages[j].getSubject().split("Feedback via the ")[1].split("[")[1].split("]")[0];
var dat = messages[j].getDate();
var referrer = messages[j].getPlainBody().split("Referer URL:")[1].split("\n")[0];
ss.appendRow([name, email, phone, addr, city, find, sub, num, dat, referrer])
}
threads[i].removeLabel(label);
}
}
My e-mail looks like this:
Name*: name
E-mail*: email#gmail.com
Phone:
Street Address: 3704 17th St.
City*: city
How did you hear about us?*: Search engine results
Brief description of work requested*: work here
So my code extracts the appropriate strings for each field except the 'Phone' and 'Address' fields which are not required. If these fields are not filled, then the e-mail does not have the words 'Phone' or 'Street Address' so the lines for var phone and var addr fail because you can't split a null. Is there a way to insert an if string 'Phone' and 'Street Address' exists then execute the above? Thanks.
You were right that you'll need regex to accomplish the job (or it'll certainly make it easier). I've written a simple script in Codepen that'll show you how to use the regex.
In my script, I split the body data at the newline character, and then loop through the resulting array of lines. I pipe each line into a function that captures and returns the text you need. You needn't pipe in anything other the line--it detects what the name of the field is, and uses it appropriately, based on your current format.
In your own code, you would have to do the following to msg before placing it into your spreadsheet:
var msg = messages[j].getPlainBody();
var sub = messages[j].getSubject();
var dat = messages[j].getDate();
var bodyLines = msg.split("\n");
var fields = [];
for (var k = 0; k < bodyLines.length; k++) {
fields.push(getText(bodyLines[k]));
}
// do something with the resulting array of fields here
Here's the getText(str) function (can also be found in Codepen):
function getText(str) {
var fieldRe = new RegExp("(.+)\:", "g");
var fieldGroups = fieldRe.exec(str);
var fieldName = fieldGroups[1].split("*")[0];
fieldName = (fieldName == null) ? fieldGroups[1] : fieldName;
fieldName = fieldName.replace(/[\!\#\#\$\%\^\&\*\(\)\-\_\+\=\`\~\[\]\{\}\\\/\|\:\;\'\"\<\>\,\.\?]/g, function transformIllegal(x) {
return "\\" + x;
});
var re = new RegExp(`${fieldName}\\*?\\:\\s+(.*)`, "g");
var groups = re.exec(str);
var out = (groups == null) ? "" : groups[1];
return out;
}
Here's what I'm ending with. Not sophisticated but works.
function emf() {
var ss = SpreadsheetApp.getActiveSheet();
var label = GmailApp.getUserLabelByName("tkh_emf");
var threads = label.getThreads();
for (var i=0; i<threads.length; i++)
{
var messages = threads[i].getMessages();
for (var j=0; j<messages.length; j++)
{
var name = messages[j].getPlainBody().split("Name*:")[1].split("\n")[0];
var email = messages[j].getPlainBody().split("E-mail*:")[1].split("\n")[0];
try {var phone = messages[j].getPlainBody().split("Phone:")[1].split("\n")[0];}
catch(e){var phone = "-";}
try {var addr = messages[j].getPlainBody().split("Street Address:")[1].split("\n")[0];}
catch(e){var addr = "-";}
var city = messages[j].getPlainBody().split("City*:")[1].split("\n")[0];
var find = messages[j].getPlainBody().split("hear about us?*:")[1].split("\n")[0];
try {var referrer = messages[j].getPlainBody().split("Referrer Name:")[1].split("\n")[0];}
catch(e){var referrer = "-";}
var sub = messages[j].getSubject().split("Feedback via the ")[1].split("[")[0];
var num = messages[j].getSubject().split("Feedback via the ")[1].split("[")[1].split("]")[0];
var dat = messages[j].getDate();
ss.appendRow([name, email, phone, addr, city, find, referrer, sub, num, dat])
}
threads[i].removeLabel(label);
}
}

I need to get values from a url after (#) with jquery

I have this url:
http://test.words.aspx#word_id=1034374#lang_code=en
I need to get the values of word_id and Language code and assign them to variables.
var word_id = 1034374;
var lang_kod = en;
Here is the code you need:
var link = window.location;
var data = link.split("#");
var word_id = data[1].split("=")[1];
var lang_code = data[2].split("=")[1];
With window.location you get the url that you currently are (an alternative is document.location.
Then you split it by the hash symbol (link.split("#")) and the result is stored as an array of strings (data) which now contains http://test.words.aspx at index 0, word_id=1034374 at index 1 and lang_code=en at index 2;
What you have to do now is split the string at index 1 by the equals symbol (data[1].split("=")). This also is an array of string which has the word_id at index 0 and 1034374 at index 1 which is what you need (data[1].split("=")[1]).
Following the same logic you also get the lang_code variable.
Test it here:
var link = "http://test.words.aspx#word_id=1034374#lang_code=en"
var data = link.split("#");
var word_id = data[1].split("=")[1];
var lang_code = data[2].split("=")[1];
console.log("Word ID = " + word_id);
console.log("Lang Code = " + lang_code);
Hope this was helpful :)
You should really only have one URL fragment. But in your case you could do the following to achieve this.
var url = "http://test.words.aspx#word_id=1034374#lang_code=en"; // or use window.location;
var url_splitted = url.split('#');
alert((url_splitted[1]).split('=')[1]); // showing you the values
alert((url_splitted[2]).split('=')[1]); // showing you the values
var work_id = (url_splitted[1]).split('=')[1];
var lang_code = (url_splitted[2]).split('=')[1];
Working JSFiddle: https://jsfiddle.net/o0nwyvfz/
Note:
If you are about to use multiple URL fragments, I'd suggest you to use URL parameters e.g.: http://test.words.aspx?word_id=1034374&lang_code=en
Which you could retrieve by:
A solution provided by: http://www.jquerybyexample.net/2012/06/get-url-parameters-using-jquery.html
var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
};
And this is how you can use this function assuming the URL is,
http://test.words.aspx?word_id=1034374&lang_code=en
var tech = getUrlParameter('word_id');
var blog = getUrlParameter('lang_code');

How to get the parameter value from URL in Jquery?

Hi all i have an url where i need to get an parameter from the url
var URL="http://localhost:17775/Students/199/Kishore"
//here from the url i need to get the value 199
this is what i had been trying but the value is null here
function getURLParameter(name) {
return parent.decodeURI((parent.RegExp(name + /([^\/]+)(?=\.\w+$)/).exec(parent.location.href) || [, null])[1]);
};
$(document).ready(function() {
getURLParameter("Students");
//i need to get the value 199 from the url
});
jQuery is not needed for this, though it could be used. There are lots of ways to skin this cat. Something like this should get you started in the right direction:
var URL="http://localhost:17775/Students/199/Kishore";
var splitURL = URL.split("/");
var studentValue = "";
for(var i = 0; i < splitURL.length; i++) {
if(splitURL[i] == "Students") {
studentValue = splitURL[i + 1];
break;
}
}
Here's a working fiddle.
Edit
Based on the comments, indicating that the position will always be the same, the extraction is as simple as:
var url = "http://localhost:17775/Students/199/Kishore";
var studentValue = url.split("/")[4];
This is what you're looking for since the URL parameter will keep changing:
http://jsbin.com/iliyut/2/
var URL="http://localhost:17775/Students/199/Kishore"
var number = getNumber('Students'); //199
var URL="http://localhost:17775/Teachers/234/Kumar"
var number = getNumber('Teachers'); //234
function getNumber(section) {
var re = new RegExp(section + "\/(.*)\/","gi");
var match = re.exec(URL);
return match[1];
}
I would do the following:
var url = "http://localhost:17775/Students/199/Kishore";
var studentValue = url.match('/Students/(\\d+)/')[1]; //199

Categories

Resources