Split url with javascript from position - javascript

I need to parse a url:
http://localhost.com/kw-webapp/preview/cn/1/cmm/Default/13203/content.13203.1.1.html?context=
var url = window.location.pathname.split( '/' );
var _language = 4;
var _layoutuid = 5;
var _themeuid = 6;
var _contentuid = 7;
var _content = 8;
var language = url[_language];
var layoutuid = url[_layoutuid];
var themeuid = url[_themeuid];
var contentuid = url[_contentuid];
var content = url[_content];
But I need to parse the url from this position:
cn/1/cmm/Default/13203/content.13203.1.1.html?context=
and variables have to be:
var _language = 0; //1
var _layoutuid = 1; //cmm
var _themeuid = 2; //Default
var _contentuid = 3; //13203
var _content = 4;
My problem is, url can start like this:
www.localhost.com/kw-webapp/kw/preview/cn/1/cmm/Default/13203/content.13203.1.1.html?context=
or this:
www.localhost.com/preview/cn/1/cmm/Default/13203/content.13203.1.1.html?context=
How can I parse the url from
cn/1/cmm/Default/13203/content.13203.1.1.html?context=?

If your URL is always of the form *preview/cn/* and you want the last part of it, you can trim the URL using RegExp like below:
var trimmedURL = "http://localhost.com/kw-webapp/preview/cn/1/cmm/Default/13203/content.13203.1.1.html?context=".replace(/.*preview\/cn\//,'');
//console.log(trimmedURL);
var url = trimmedURL.split( '/' );
Demo Fiddle

Related

How to use the result of an IF statement as a variable

I have a code as follows:
function DetailFacture2() {
var ss = SpreadsheetApp.getActive();
var DetailDEVIS = SpreadsheetApp.setActiveSheet(ss.getSheetByName('DetailDEVIS'));
var FACTUREDevis = SpreadsheetApp.setActiveSheet(ss.getSheetByName('FACTUREDevis'));
var DetailFactureDevis = SpreadsheetApp.setActiveSheet(ss.getSheetByName('DetailFactureDevis'));
var lastrowpaste = FACTUREDevis.getLastRow();
var numrow = FACTUREDevis.getRange(lastrowpaste,13).getValue()
var lastrowpaste2 = DetailFactureDevis.getLastRow() - numrow +2;
var data = DetailDEVIS.getDataRange().getValues();
var DetailD = FACTUREDevis.getRange(lastrowpaste,2).getValue();
for(var i = 0; i<data.length;i++){
if(data[i][1] == DetailD){ //[1] because column B
var firstrowcopy = i+1;
Logger.log(firstrowcopy)
return (firstrowcopy)
}
}
};
It does return the correct value, but how do you use "firstrowcopy" as a fixed var?
I would like to use as follows:
function DetailFacture2() {
var ss = SpreadsheetApp.getActive();
var DetailDEVIS = SpreadsheetApp.setActiveSheet(ss.getSheetByName('DetailDEVIS'));
var FACTUREDevis = SpreadsheetApp.setActiveSheet(ss.getSheetByName('FACTUREDevis'));
var DetailFactureDevis = SpreadsheetApp.setActiveSheet(ss.getSheetByName('DetailFactureDevis'));
var lastrowpaste = FACTUREDevis.getLastRow();
var numrow = FACTUREDevis.getRange(lastrowpaste,13).getValue()
var lastrowpaste2 = DetailFactureDevis.getLastRow() - numrow +2;
var data = DetailDEVIS.getDataRange().getValues();
var DetailD = FACTUREDevis.getRange(lastrowpaste,2).getValue();
for(var i = 0; i<data.length;i++){
if(data[i][1] == DetailD){ //[1] because column B
var firstrowcopy = i+1;
var source = DetailDEVIS.getRange(firstrowcopy,1,numrow-1);
var destination = DetailFactureDevis.getRange(lastrowpaste2,3);
source.copyTo(destination);
}
}
};
But, as one would expect, it cannot work as it loops...
Not sure if I understand your question too. The code doesn't look well. Here is just my guess. Try to change the last lines this way:
// ...
var firstrowcopy = 0;
for (var i = 0; i < data.length; i++){
if(data[i][1] == DetailD){ //[1] because column B
firstrowcopy = i+1;
break;
}
}
var source = DetailDEVIS.getRange(firstrowcopy,1,numrow-1);
var destination = DetailFactureDevis.getRange(lastrowpaste2,3);
source.copyTo(destination);
}

JavaScript - How to increase count

when I give the variable which is an IP(10.31.68.0/22) with the current below script it will return
Output: 10.31.68.1 - 10.31.68.10
var network = '10.31.68.0/22';
var IPstart = network.toString().substring(0,network.lastIndexOf('.')) + ".1";
var IPend = network.toString().substring(0,network.lastIndexOf('.')) + ".10";
var excludename = IPstart+"-"+IPend;
I am looking for something (Output) like below. 68 placeholder should increase by +1 like below.
10.31.68.1 - 10.31.68.10
10.31.69.1 - 10.31.69.10
10.31.70.1 - 10.31.70.10
10.31.71.1 - 10.31.71.10
You Need to split and join the values with loop
var network = '10.31.68.0/22';
var IPstart = network.toString().substring(0,network.lastIndexOf('.')) + ".1";
var IPend = network.toString().substring(0,network.lastIndexOf('.')) + ".10";
var excludename = IPstart+"-"+IPend;
var upToNumber=4;
for(var i=0;i<upToNumber;i++){
var res_ipstart = IPstart.split(".");
res_ipstart[2]=parseInt(res_ipstart[2]) + i;
var ip_start = res_ipstart.join(".");
var res_ipend = IPend.split(".");
res_ipend[2]=parseInt(res_ipend[2]) + i;
var ip_end = res_ipend.join(".");
var output=ip_start+"-"+ip_end;
console.log(output);
}
This should do:
/* Remove the slash */
let rawIp = "10.31.68.1/22";
let rawIpParts = rawIp.split("/");
/* Split the new ip */
let ip = rawIpParts[0];
let ipParts = ip.split(".")
/* Append 0 */
ipParts[3] = ipParts[3].concat("0");
/* Output Array */
let output = []
/* Loop and increment */
for(let index = 0; index <= 4; index++) {
ipParts[2] = 1 + +ipParts[2];
output.push(ipParts.join("."));
}
/* Print */
console.log(output);
Hope this helps!

Why Won't My Hidden Field Populate from JavaScript Function?

I created a JavaScript function that creates a string that I need to use in another form on the website I'm working on. To do this I thought I would just write the value to a hidden field, and then submit it in the HTML form. However, nothing I do will get the value to appear in the field even though the alert will populate.
function customFunc(){
var customD = document.querySelector('input[name = "customD"]:checked').value;
var customL = document.getElementById('Custom_Length').value;
var customW = document.getElementById('Custom_Width').value;
var lowerV = customL * customW;
var maxV1 = Math.ceil(lowerV/100)*100;
var maxV2 = maxV1 - 1;
var lowerB = maxV1 - 100;
var pStyle = document.querySelector('input[name = "pl"]:checked').value;
var itemTest = pStyle +customD+"."+lowerB+"-"+maxV2;
alert(itemTest );
return itemTest ;
document.getElementById('testSku').value = itemTest ;
}
On the HTML side I have this as the hidden field
<input type="text" name="testSku" id="testSku">
The return statement ends a function and returns to the caller. Anything after that in the function is not executed. So the assignment to the input's value is ignored. Put the return statement last.
function customFunc(){
var customD = document.querySelector('input[name = "customD"]:checked').value;
var customL = document.getElementById('Custom_Length').value;
var customW = document.getElementById('Custom_Width').value;
var lowerV = customL * customW;
var maxV1 = Math.ceil(lowerV/100)*100;
var maxV2 = maxV1 - 1;
var lowerB = maxV1 - 100;
var pStyle = document.querySelector('input[name = "pl"]:checked').value;
var itemTest = pStyle +customD+"."+lowerB+"-"+maxV2;
alert(itemTest );
document.getElementById('testSku').value = itemTest ;
return itemTest ;
}

Loop through elements with greasemonkey

I have the following working code on greasemonkey:
var qtt = 100;
var filler1 = document.getElementById("s1_0");
var filler2 = document.getElementById("s2_0");
var filler3 = document.getElementById("s3_0");
var filler4 = document.getElementById("s4_0");
var filler5 = document.getElementById("s5_0");
var filler6 = document.getElementById("s6_0");
var filler7 = document.getElementById("s7_0");
filler1.value = qtt;
filler2.value = qtt;
filler3.value = qtt;
filler4.value = qtt;
filler5.value = qtt;
filler6.value = qtt;
filler7.value = qtt;
Where the "sN_0" are the names of inputs, the code works, but I was wondering if there is a better way to do the same, to loop through all the id names or something.
here is a simple loop doing that your code does
var qtt=100;
for (var i =1;i<8;i++){
document.getElementById("s"+i+"_0").value=qtt
}
If you can use jQuery, try the following.
var qtt = 100;
$('[id]').each(function() {
if(/^s\d_0$/.test(this.id)) {
this.value = qtt;
}
});
Demo

URL Bar id=1 Into jQuery ID

i want to get the id from the url bar and insert it into the href
$("a[href='send_message.php?act=pm&id=$id']").colorbox({width:"500", height:"350", iframe:true});
there's a jquery plugin to make this ridiculously simple:
see: http://plugins.jquery.com/project/query-object
e.g.
var id = $.query.get('id');
$("a[href='send_message.php?act=pm&id="+id+"']").colorbox({width:"500", height:"350", iframe:true});
For those not using jQuery or any other JS library:
var searchString = document.location.search;
// strip off the leading '?'
searchString = searchString.substring(1);
var gvPairs = searchString.split("&");
var getVars = [];
for (i = 0; i < gvPairs.length; i++)
{
var gvPair = gvPairs[i].split("=");
getVars[gvPair[0]] = gvPair[1];
}
So if the URL string was index.php?id=3&page=2&display=10 then:
getVars['id'] = 3;
getVars['page'] = 2;
getVars['display'] = 10;

Categories

Resources