How to get values from link [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am passing Latitude and Longitude by link to direction.html file
.../direction.html?latlng=53.456269068499545,-6.220780313014984
how can I get this values to get lat and lng separately

You can use hashtag, that is passing the value latitude and longitude like this
ursite/path#53.456269068499545,-6.220780313014984
var tag = window.location.hash;
tag = tag.substring(1,tag.length);
var co_ordinate = tag.split(",")
var latlng = co_ordinate[0]
var logitude = co_ordinate[1];

Because you're passing a single variable, you could just split the data using the comma. The result would be an array.
latlng=53.456269068499545,-6.220780313014984
var res = latlng.split(",");
http://www.w3schools.com/jsref/jsref_split.asp

Well Im not sure what language you are using to script the page. I have ideas for PHP.
//you get the variable being passed
if (isset ($_Get['longlat'];) {
$longlat = $_GET['longlat'];
//then you split them with explode
$longlat = explode(',', $longlat);
$long = $longlat[0];
$lat = $longlat[1];
thats about it. read the manual on explode to get the correct syntax, but this is the gist of what you want i believe

You can use split to pull the string into an array, splitting it by the comma, then just assign each of the array's values to a new variable for lat and lng.
I would also parse the data into floats if you are using it as actual coordinate data later on, rather than just printing it.
For:
String latlng=53.456269068499545,-6.220780313014984;
Use:
var coords = latlng.split(",");
float lat = parseFloat(coords[0]);
float lng = parseFloat(coords[1]);

Related

.shift() apparently no longer exists [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
https://jsfiddle.net/a/2L4t9saq/217/ is my fiddle
most of the code you can ignore, here is the function:
var modGrid = function(code){
var arr = code
console.log(arr)
for(var n=1;n<gridx+1;n++){
for(var i = 1; i<gridy+1; i++){
var garbledMess = "[x="+i+"][y="+n+"]"
var idea = arr[0]
arr.shift()
$(garbledMess).css("background-color",idea)
}
}
}
the syntax error is as follows:
Uncaught TypeError: arr.shift is not a function
at modGrid ((index):44)
at window.onload ((index):81)
since the modGrid function takes in an array (in the case of my code an array of 4 elements) the .shift() function should be removing the first option in the array, it worked before i added some more code, but now it is apparently not a function
many thanks
since the modGrid function takes in an array
It is designed to take an array, but that isn't what you are passing it.
You are passing it a string, another string, a number and another number.
modGrid('rgba(255,0,0,1)','rgba(0,255,0,1)',2,1);

converting python code to javascript code [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
can somebody help for my code which is written in python, i want to write it in javascript but im in trouble, i dont know how.
python code
cities={}
for line in open("linnadkaugustega.txt", "r", encoding="UTF-8"):
m=line.strip().split()
abim=[word.split(":") for word in m[1:]]
cities[m[0]]={}
for couple in abim:
cities[m[0]][couple[0]]=int(couple[1])
print(cities);
and i tried in javascript but that doesen't work
function tere(){
console.log("Tere");
$.get('read.txt', function(data) {
cities={};
var lines = (data.trim()).split();
abim=[var word.split(":") for word in m[1:]]
cities[m[0]]={};
for var couple in abim
cities[m[0]][couple[0]]=couple[1];
console.log(cities);
}, 'text');
}
tere();
can somebody help me ?
You have syntax issues translating from python to js. Heres how arrays work...
if you have an array litteral in javascript
var cities = [];
Then we would add to the array by calling push
cities.push('Portland');
...
cities.push('New York');
we can then iterate over the array by calling forEach on the array object.
cities.forEach(function (city, index){
//do work on each city
console.log(city);
});
// Portland
// New York
A few things:
.split() in JS does something different than split in python when no separator is given. To split a line into words, you'll need to split on whitespaces explicitly
you're missing the for loop over the lines of the file. Python uses the iterator syntax for reading from the file, in JavaScript an ajax request loads the whole file and you'll need to split it in lines yourself.
JavaScript does not have that m[1:] syntax, you'll need to use the .slice() method instead
JavaScript does not have array/list comprehensions. You will need to use an explicit loop, or the map method of arrays
your loop syntax is too pythonic. In JavaScript, for loops need parenthesis and an index variable.
So this should do (supposed you have the jQuery library loaded and it finds the file):
$.get('read.txt', function(data) {
var cities = {};
var lines = data.split("\n");
for (var i=0; i<lines.length; i++) {
var line = lines[i];
var m = line.trim().split(/\s+/);
var abim = m.slice(1).map(function(word) {
return word.split(":");
});
var obj = cities[m[0]] = {};
for (var j=0; j<abim.length; j++) {
var couple = abim[j];
obj[couple[0]] = couple[1];
}
}
console.log(cities);
}, 'text');

How to UnObfuscate, Obfuscated Javascript? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I obfuscated my javascript code using this one http://www.javascriptobfuscator.com/Default.aspx but it seems I can' find way to back it to original code.. is there any way?
You can get back some of your code, but almost all function names will be lost and local variables tend to get replaced.
Take for example the default code at the link you provided:
ORIGINAL
function NewObject(prefix)
{
var count=0;
this.SayHello=function(msg)
{
count++;
alert(prefix+msg);
}
this.GetCount=function()
{
return count;
}
}
var obj=new NewObject("Message : ");
obj.SayHello("You are welcome.");
OBFUSCATED
var _0x5601=["\x53\x61\x79\x48\x65\x6C\x6C\x6F","\x47\x65\x74\x43\x6F\x75\x6E\x74","\x4D\x65\x73\x73\x61\x67\x65\x20\x3A\x20","\x59\x6F\x75\x20\x61\x72\x65\x20\x77\x65\x6C\x63\x6F\x6D\x65\x2E"];function NewObject(_0xa158x2){var _0xa158x3=0;this[_0x5601[0]]=function (_0xa158x4){_0xa158x3++;alert(_0xa158x2+_0xa158x4);} ;this[_0x5601[1]]=function (){return _0xa158x3;} ;} ;var obj= new NewObject(_0x5601[2]);obj.SayHello(_0x5601[3]);
STEP 1 - Decode variable array
Firstly we need to decode the variable array (the part that starts var _0x5601= and ends just before the first function). I find the easiest way to do this is to copy and paste the array into Chromes developer console. Just paste the whole line and hit enter, then in the console type the variable name and you'll get something like this:
["SayHello", "GetCount", "Message : ", "You are welcome."]
STEP 2 - String Replace code for variable array item
Next we employ the help of whichever programming language you'd like to parse this new array back into the js. In essence, take your newly decoded array, and perform a string replace the rest of the code. I had PHP handy, so i did this:
<?php
// decoded array
$_0x5601 = array("SayHello", "GetCount", "Message : ", "You are welcome.");
// rest of the obfuscated code
$code = "function NewObject(_0xa158x2){var _0xa158x3=0;this[_0x5601[0]]=function (_0xa158x4){_0xa158x3++;alert(_0xa158x2+_0xa158x4);} ;this[_0x5601[1]]=function (){return _0xa158x3;} ;} ;var obj= new NewObject(_0x5601[2]);obj.SayHello(_0x5601[3]);";
// loop over array
for($x = 0; $x < count($_0x5601); $x++){
// string replace on the code
$code = str_replace('_0x5601['.$x.']', '"'.$_0x5601[$x].'"', $code);
}
// output result
echo $code;
?>
STEP 3 - BEAUTIFY
Now, lets "beautify" the code using something like: http://jsbeautifier.org/
function NewObject(_0xa158x2) {
var _0xa158x3 = 0;
this["SayHello"] = function(_0xa158x4) {
_0xa158x3++;
alert(_0xa158x2 + _0xa158x4);
};
this["GetCount"] = function() {
return _0xa158x3;
};
};
var obj = new NewObject("Message : ");
obj.SayHello("You are welcome.");
STEP 4 - Regex replace array items with object notation
The last step is to perform one last replace, but this time we need to employ the help of regex. I use an IDE called Sublime Text 2 that has the ability to do find and replace regex (im sure most IDE's have this too).
The regex pattern i used looks like this \[\"([a-zA-Z0-9\-\_]+)\"\] to explain:
\[\" // code must start with ["
( // open capturing group
[a-zA-Z0-9]+ // match all characters a-zA-Z0-9 you may need to adjust this to include -, _ etc as needed
) // capture everything in this group
\"\] // code must end with "]
You want to replace anything that matches this pattern with .$1. Resulting in:
function NewObject(_0xa158x2) {
var _0xa158x3 = 0;
this.SayHello = function(_0xa158x4) {
_0xa158x3++;
alert(_0xa158x2 + _0xa158x4);
};
this.GetCount = function() {
return _0xa158x3;
};
};
var obj = new NewObject("Message : ");
obj.SayHello("You are welcome.");
It's not quite as pretty, and as i mentioned local variables have been replaced. But if you know your code it shouldnt be too difficult to understand what they are doing.

how to get javascript variable value in different variable [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have 2-9 value stored in a single variable in JavaScript.
But I want 2 to be stored in a variable and 9 stored in b variable.
For example :
<script>
var a = "2-9";
</script>
And I want to,
<script>
var a = "2";
var b = "9";
</script>
Please help me.
You could use the charAt() method of String in javascript.
var a = string.charAt(0);
var b = string.charAt(2);
where string is "2-9".
I suppose that your string, wouldn't have the form "2-10", because in this case that will not work.
If that's the case then you have to use the split() method of String in javascript.
var c = string.split('-');
var a = c[0];
var b = c[1];
For futher documentation about these methods in javascript plese refer to the following links:
JavaScript String charAt() Method
JavaScript String split() Method
use .split() in javascript
var a = "2-9";
var c = a.split("-");
a = c[0]; // it returns 2
var b = c[1]; // it returns 9
You can use .split():
The split() method splits a String object into an array of strings by
separating the string into substrings.
var a = "2-9",
b = a.split('-')[1];
a = a.split('-')[0];
Fiddle Demo
What you can do is this:
<script>
var string_value = "2-9".split('-'),
minimum_name = string_value[0], // 2
maximum_name = string_value[1]; // 9
</script>
Make use of .split() which creates an array out of a string, so you can split it by - then you can assign the values with their indexes like [0], [1].

Converting string to array object using javascript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I convert the following string using JavaScript to array:
from: var x = "{id:'2'},{name:'code,Barer'}";
to: var x1 = [{id:"2"},{name:"code,Barer"}];
If you want that exact string to be an array object you can do this:
var x = "{id:'2'},{name:'code,Barer'}";
var newArray = eval("[" + x + "]");
Here are some the dangers of eval: Why is using the JavaScript eval function a bad idea?
How are you getting the variable x? If you can get whatever it is to spit out valid JSON (using JSON.stringify or something similar) you can then parse it correctly into JS (although some implementations of JSON parsers do use eval).
If you want to avoid using eval for security reasons try this
var string = "{id:'2'},{name:'code,Barer'}",
array = string.substr(1, string.length - 2)
.split("},{")
.map(function(item){
item = item.split(":");
var result = {},
name = item[0],
value = item[1].replace(/'/g, "");
result[name] = value;
return result
});

Categories