Hi everyone I'm beginner CTF player and I have same lake with javascript and I face following problem
I couldn't understand what is the intention of following line
location.search.match(/e=(.*)/)[1]))
This is complete code
if(location.search) {
var div = document.currentScript.parentNode.appendChild(document.createElement('div'));
div.className = 'alert alert-danger';
div.role = 'alert';
div.appendChild(document.createTextNode(unescape(location.search.match(/e=(.*)/)[1])));
}
can u help me to understand it
location.search basically give you the query string part of the current URL and and match has been used to extract e parameter value from the query string with a regular expression.
As an example, if the current URL is like
https://www.example.com/?e=someone#example.com then location.search.match(/e=(.*)/)[1] will give you 'someone#example.com'.
Rest of the code basically create a div element and set extracted text as a child of it and finally append that div as a child of parent node of the currently running script tag.
Here is my working example:
This URL have multiple query parameters, such as: config=xxx & zoom=17 & lat=xxx etc...
I want to extract each query parameters one by one.
http://localhost:10/mapserver1/viewer/?config=viewer_simple1&mapserver_url=https://maps2.dcgis.dc.gov/dcgis/rest/services/Zoning/MapServer&zoom=17&lat=38.917292&long=-77.036420
This is how you do (working code):
var ___zoom = location.search.match(/zoom=([^&]*)/i)[1];
var ___lat = location.search.match(/lat=([^&]*)/i)[1];
var ___long = location.search.match(/long=([^&]*)/i)[1];
var ___basemap = location.search.match(/basemap=([^&]*)/i)[1];
var ___type = location.search.match(/type=([^&]*)/i)[1];
var ___url = location.search.match(/url=([^&]*)/i)[1];
var ___title = location.search.match(/title=([^&]*)/i)[1];
var ___opacity = location.search.match(/opacity=([^&]*)/i)[1];
//console.log(location.search.match(/zoom=([^&]*)/i)[0]); // 'zoom=17'
//console.log(location.search.match(/zoom=([^&]*)/i)[1]); // '17'
console.log(___zoom);
console.log(___lat);
console.log(___long);
console.log(___basemap);
console.log(___type);
console.log(___url);
console.log(___title);
console.log(___opacity);
This is my fully working code:
first check if parameter exist, if does, then extract it.
var ___zoom;
var ___lat;
var ___long;
var ___basemap;
var ___type;
var ___url;
var ___title;
var ___opacity;
/*
* if (value) {
*
* }
*
* will evaluate to true if value is not:
null
undefined
NaN
empty string ("")
false
0
*
*
*
*/
if ( location.search.match(/zoom=([^&]*)/i) )
{
___zoom = location.search.match(/zoom=([^&]*)/i)[1];
}
if ( location.search.match(/lat=([^&]*)/i) )
{
___lat = location.search.match(/lat=([^&]*)/i)[1];
}
if (location.search.match(/long=([^&]*)/i))
{
___long = location.search.match(/long=([^&]*)/i)[1];
}
if (location.search.match(/basemap=([^&]*)/i))
{
___basemap = location.search.match(/basemap=([^&]*)/i)[1];
}
if (location.search.match(/type=([^&]*)/i))
{
___type = location.search.match(/type=([^&]*)/i)[1];
}
if (location.search.match(/url=([^&]*)/i))
{
___url = location.search.match(/url=([^&]*)/i)[1];
}
if (location.search.match(/title=([^&]*)/i))
{
___title = location.search.match(/title=([^&]*)/i)[1];
}
if (location.search.match(/opacity=([^&]*)/i))
{
___opacity = location.search.match(/opacity=([^&]*)/i)[1];
}
//console.log(location.search.match(/zoom=([^&]*)/i)[0]); // 'zoom=17'
//console.log(location.search.match(/zoom=([^&]*)/i)[1]); // '17'
console.log(___zoom);
console.log(___lat);
console.log(___long);
console.log(___basemap);
console.log(___type);
console.log(___url);
console.log(___title);
console.log(___opacity);
Related
I have a column in csv file named event_name and I am using Modified Java Script Value step to create new columns if the first character of event_name fufill conditions , for example :
event_name ="app_connection: connection_start_time = 10/05/2018 17:29:26: connection_end_time = 10/05/2018 17:29:29: connection_duration_in_seconds = 3"
==> event = app_connection
==> connection_start_time = 10/05/2018 17:29:26
==> connection_end_time = 10/05/2018 17:29:29
==> connection_duration_in_seconds = 3
if it's : event_name ="scene_access"
==> event = scene_access
I tried this code but it doesn't seem to change anything :
if(event_name.substr(0,3).equals("app"))
{
var event = event_name.substring(0,14);
var connection_start_time = event_name.substr(40,59);
var connection_end_time = event_name.substr(83,102);
var connection_duration_in_seconds = event_name.substr(137,139);
}
else
{
event_name = event_name;
}
If you could give me a hint or explaining what I'm missing will be a huge help.
Thank you.
I tried the field in the substring function, and in the substr function the sintaxis should be substr(string, position_from, number_of_characters).
With this change the code should be:
if(substr(event_name,0,3).equals("app"))
{
var event = substr(event_name,0,14);
var connection_start_time = substr(event_name,40,19);
var connection_end_time = substr(event_name,83,19);
var connection_duration_in_seconds = substr(event_name,137,1);
}
else
{
event_name = event_name;
}
After this press the button "Get variables" to make available the fields in the output of the step.
I am busy with making a kind of invoice system, where the user can make invoices very easily. Now I am at the point where I have to count up, per product, three different variables/items, but instead of counting them up, my javascript code puts it like text (with the + operator).
Example:
selectmenu 1 = option 0 (where VAT = 8.50 euro's)
selectmenu 2 = option 1 (where VAT = 12.76 euro's)
Now the output has to be (8.50+12.76)= 21.26
The output in my situation is = 8.5012.76
My (partial) javascript code:
$("select#product").on("change", function (e) {
var $row = $(e.target).closest('.productitem');
var selVal = $row.find('#product').val();
var totalvat;
var currentVat = $('#totalvat').val();
var NLhoog = 1.21;
var price0EXC = 40.49;
var price0INC = (price0EXC * NLhoog).toFixed(2);
var price0VAT = (price0INC - price0EXC).toFixed(2);
var price1EXC = 60.74;
var price1INC = (price1EXC * NLhoog).toFixed(2);
var price1VAT = (price1INC - price1EXC).toFixed(2);
if (selVal === "0") {
$row.find("input#vat").val(price0VAT);
$row.find("input#priceEXC").val(price0EXC);
$row.find("input#priceINC").val(price0INC);
totalvat = (currentVat + price0VAT);
$('input#totalvat').val(totalvat);
} else if (selVal === "1") {
$row.find("input#vat").val(price1VAT);
$row.find("input#priceEXC").val(price1EXC);
$row.find("input#priceINC").val(price1INC);
totalvat = currentVat+price1VAT;
$('input#totalvat').val(totalvat);
}
});
I have let the unimportant part of the code away.
If you know what I am doing wrong, please let me know :)
Think this may help?
var currentVat = parseFloat($('#totalvat').val());
You are using var currentVat = $('#totalvat').val(); to get the value from an input I assume? This is a string which will need to be parsed at some to a relevant datatype. When + is used with a string the compiler performs concatenation.
Try something like:
var currentVat = parseFloat($('#totalvat').val());
Or do it later on with:
parseFloat(currentVat);
As you're using numbers as currency I'd consider adding the suffix .ToFixed(2) at the end, and maybe some other formatting
I am trying to output only articles if authorsId = authorId.
Beside that the whole function works exactly as I want, here it is:
The general idea is to limit access to only own articles.
So, my question is: how do I limit the results to show only articles written by the owner of the page we are on (authorsId = authorId).
function ArticlesListReturn(returned) {
xml = returned.documentElement;
var rel = document.getElementById('related_category_articles');
rel.options.length = 0;
var status = getXMLData('status');
var title = '';
var id = '';
var authorid = '';
if (status == 0) {
alert("%%LNG_jsArticleListError%%" + errormsg);
} else {
var authorid = document.getElementById("authorid").value; // Serge
// authorsid = getNextXMLData('authors',x);
for (var x = 0; x < xml.getElementsByTagName('titles').length; x++) {
title = getNextXMLData('titles', x);
id = getNextXMLData('ids', x);
authorsid = getNextXMLData('authors', x);
alert(authorsid) // authors of each article - it returns the proper values
alert(authorid) // author of the page we are on - it returns the proper value
var count = 0;
rel.options[x] = new Option(title, id, authorid); // lign that returns results
title = '';
id = '';
authorid = '';
}
}
I suspect the problem is when you try performing a conditional statement (if/then/else) that you are comparing a number to a string (or a string to a number). This is like comparing if (1 == "1" ) for example (note the double quotes is only on one side because the left would be numeric, the right side of the equation would be a string).
I added a test which should force both values to be strings, then compares them. If it still gives you problems, make sure there are no spaces/tabs added to one variable, but missing in the other variable.
Also, I changed your "alert" to output to the console (CTRL+SHIFT+J if you are using firefox). The problem using alert is sometimes remote data is not available when needed but your alert button creates a pause while the data is being read. So... if you use alert, your code works, then you remove alert, your code could reveal new errors (since remote data was not served on time). It may not be an issue now, but could be an issue for you going forward.
Best of luck!
function ArticlesListReturn(returned) {
xml = returned.documentElement;
var rel = document.getElementById('related_category_articles');
rel.options.length = 0;
var status = getXMLData('status');
var title = '';
var id = '';
var authorid = '';
if (status == 0) {
alert("%%LNG_jsArticleListError%%" + errormsg);
} else {
var authorid = document.getElementById("authorid").value; // Serge
// authorsid = getNextXMLData('authors',x);
for (var x = 0; x < xml.getElementsByTagName('titles').length; x++) {
title = getNextXMLData('titles', x);
id = getNextXMLData('ids', x);
authorsid = getNextXMLData('authors', x);
console.log("authorsid = "+authorsid); // authors of each article - it returns the proper values
console.log("authorid = "+authorid); // author of the page we are on - it returns the proper value
if( authorsid.toString() == authorid.toString() )
{
rel.options
var count = 0;
console.log( authorsid.toString()+" equals "+authorid.toString() );
rel.options[rel.options.length] = new Option(title, id, authorid); // lign that returns results
}
else
{
console.log( authorsid.toString()+" NOT equals "+authorid.toString() );
}
title = '';
id = '';
authorid = '';
}
Did you check the console for messages? Did it correctly show authorid and authorsid?
I have edited the script and made a couple of additions...
The console will tell you if the conditional check worked or not (meaning you will get a message for each record). See the "if/else" and the extra "console.log" parts I added?
rel.options[x] changed to equal rel.options[rel.options.length]. I am curious on why you set rel.options.length=0 when I would instead have done rel.options=new Array();
The first if-else statement works as expected, when the address bar variables are there it does the if part and when not there does the else...however, right after the VAR lines that split everything up every else statement i put in does not work
var hasVars = 0;
var loc = window.location.href;
if(loc.indexOf('?')>-1) { hasVars = 1; }
if(hasVars==1) { alert("ttt"); }
else { alert("eee"); }
var query = loc.slice(loc.indexOf('?') + 1);
var vars = query.split('&');
var pair = vars[0].split('=');
var data1 = pair[1].split('.');
if(hasVars==1) { alert("ttt"); }
else { alert("eee"); }
I commented every line out and put them back in and the problem seems to be with the data1 variable line, commented out it works fine but with it the else statement doesn't run
The var query (...) part of your code will only work when there are query parameters. So it is having problems when there are none. One solution would be to place an if there as well.
var hasVars = 0;
var loc = window.location.href;
if(loc.indexOf('?')>-1) { hasVars = 1; }
if(hasVars==1) { alert("ttt"); }
else { alert("eee"); }
if (hasVars==1) { // added this
var query = loc.slice(loc.indexOf('?') + 1);
var vars = query.split('&');
var pair = vars[0].split('=');
var data1 = null;
if (pair[1] != undefined) { // added this for: "page?var" cases
data1 = pair[1].split('.');
} // added
} // added
if(hasVars==1) { alert("ttt"); }
else { alert("eee"); }
The problem you had, in detail:
When there are no query parameters, query would be just the URL. Let me explain step by step. Example page: http://example.com/page
query -> 'http://example.com/page'
vars -> ['http://example.com/page']
pair -> ['http://example.com/page']
The problem would be in:
var data1 = pair[1].split('.');
As pair has no item in pair[1]. The code above would be the same as undefined.split('.'), what gives a runtime error, thus preventing the following lines (and the second if you asked) from executing.
What exactly do you mean the statement doesn't work? Neither of the alerts is fired?
I'm guessing pair[1] does not exist and is causing an exception; can you post a sample query?
The reason the second alert doesn't fire is that query is undefined when hasVars !== 1. This causes query to be undefined, which means it is not a string and doesn't have string methods (like split). Attempting to use string methods on a non-string object throws errors, which prevent the rest of your code from running.
To solve this, define query (and the rest of your strings) as:
var query = hasVars ? loc.slice(loc.indexOf('?') + 1) : '';
var vars = hasVars ? query.split('&') : '';
var pair = hasVars ? vars[0].split('=') : '';
var data1 = hasVars ? pair[1].split('.') : '';
This uses the ternary operator (?:) to initialize query as an empty string if hasVars is not 1.
As query (and the rest of the strings) is now defined as a string one way or the other, no errors are thrown.
I want to turn a number I get from a form into a string and combine it with another string, but I receive this:
windows.location='multiSet?type=multi'Number
instead I want to get this:
windows.location='multiSet?type=multiNumber'
Note that the difference is the position of the ' mark is before Number and I want it after it..
All i see in other post is how to put a string as a number not viceversa.
var singleMultiContainer = document.getElementById("singleMultiContainer");
var singleMultiValue = singleMultiContainer.value;
var nextButton = document.getElementById("nextButton");
var multipleSetWindow = "window.location='multiSet.html?type=multi'";
var singleSetWindow = "window.location='SampleInfo.html?type=single'";
var containerCuantity = document.getElementById("containerCuantity");
function setContainers(){
singleMultiValue = singleMultiContainer.value;
containerCuantityValue = parseInt(containerCuantity.value);
if (singleMultiValue == "multi"){
//alert("Multi");
document.getElementById("nextButton").setAttribute("onclick", multipleSetWindow+containerCuantityValue);
} else if (singleMultiValue == "single") {
document.getElementById("nextButton").setAttribute("onclick", singleSetWindow);
}
}
singleMultiContainer.onchange = function(){
setContainers();
}
Don't use setAttribute to define event handlers. And you don't need to parse the value as you want to put it back in a string.
Change
document.getElementById("nextButton").setAttribute("onclick", multipleSetWindow+containerCuantityValue);
to
document.getElementById("nextButton").onclick = function(){
window.location='multiSet.html?type=multi'+containerCuantity.value;
}