JavaScript not working with putting innerhtml from databases - javascript

i have builded a java script code(here he is) :
function gup(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 results[1];
}
this code gets the permart from the head of the url and puts it into a span, now the problem is when i do "Something like that" its happend to get like that :
%22hey%20hey%22..
what can i do?
EDIT1:
I mean its takes parameters like that www.google.co.il?per="grg" and when i do www.google.co.il?per="Hey Hey" its like that %22hey%20hey%22.

try with
return decodeURIComponent(results[1]);
You will get decoded string (Readable string in the sense)

Related

Javascript regex to get param value

I'm working on a code base someone else built and the search function uses this to get the parameter from the url to place as a value to search against. I've tried re-writing it, modifiying it, and everything else possible and can't figure out what is going on with the reutrn value from the function.
Here's the function
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
console.log(name);
var regex = new RegExp("[\\?&#;]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
console.log(results);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var part = getParameterByName('part_name');
The value of location.search is ?part_number=1234&go=GO
Then in the input box part_name if I type 1234 to be the value of the passed in parameter it will only show 12. However typing in other random numbers it will sometimes show them all correctly. If I type 124 as the search param then it displays correctly as searching against 124.
Is there something wrong with one of these regex statments that could be causing this, or is this something on the server side? The PHP seems fine on the backend but I'm not savvy on the Regex used in the Javascript code.
EDIT:
A testing snippet:
function getParameterByName(loc, name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
document.getElementById("res").innerHTML += "Name: " + name + " - ";
var regex = new RegExp("[\\?&#;]" + name + "=([^&#]*)"),
results = regex.exec(loc);
document.getElementById("res").innerHTML += results + "<br/>";
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var part = getParameterByName('?part_number=1234&go=GO', 'part_number');
var part2 = getParameterByName('?part_name=12&go=GO', 'part_name');
<div id="res"/>

Get values of all the same parameter from url in jQuery

I have an url have contain two parameters.
Ex: https://www.google.com.vn/search?q=news&oq=news&aqs=chrome..69i57j69i60l3.299j0j4&sourceid=chrome&es_sm=93&ie=UTF-8#q=bbc
Parameter 1: q=news
Parameter 2:q=bbc
I want to get all value have the same parameter, but I can only get value in first the parameter.
This is my code:
function getParameterByName(name, url) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&#]" + name + "=([^&#]*)"),
results = regex.exec(url);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
Use $(location).attr('href'); and parse through the parameters yourself
what i have tried is only IDEA of how you can get all paremater.
var a="https://www.google.com.vn/search?q=news&oq=news&aqs=chrome..69i57j69i60l3.299j0j4&sourceid=chrome&es_sm=93&ie=UTF-8#q=bbc"
var b= a.split("?")[1]
b.split("&")
By this technique you will get all parameter.
You need to convert this code to your required code.
The first q is in querystring, the second one is in hash. You should parse them in JavaScript:
QueryString
// the method is taken from http://stackoverflow.com/a/3855394/3971911
var qs = (function(a) {
if (a == "") return {};
var b = {};
for (var i = 0; i < a.length; ++i)
{
var p=a[i].split('=');
if (p.length != 2) continue;
b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
}
return b;
})(window.location.search.substr(1).split('&'));
Hash
// the method is taken from http://stackoverflow.com/a/11920807/3971911
function getHashValue(key) {
return location.hash.match(new RegExp(key+'=([^&]*)'))[1];
}
Now you can use them like this:
alert(qs['q']); // news
alert(getHashValue('q')); // bbc
try this:
function getParameterByName(name, url,position) {
url=position==1?url:url.substr(url.lastIndexOf('#'));
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&#]" + name + "=([^&#]*)"),
results = regex.exec(url);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var url='https://www.google.com.vn/search?q=news&oq=news&aqs=chrome..69i57j69i60l3.299j0j4&sourceid=chrome&es_sm=93&ie=UTF-8#q=bbc';
alert(getParameterByName('q',url,1));
alert(getParameterByName('q',url,2));
Demo
Other than an exercise in coding, you're probably better off using existing code.
Something like:
https://github.com/sindresorhus/query-string
Pros:
It has tests
It's had more useful eyes cast over it (guessing here).
The people that have likely contributed to it will probably have used it.
If you find an issue with it, those people involved will have a keen interest in solving the problem as they're likely using it.
Cons:
None! :D

Insert URL parameters into Javascript code

I'm doing some conversion and revenue tracking using Virtual Website Optimizer. To track revenues, they instruct you to add a piece of Javascript code on the thank you page to determine the actual revenue earned.
<script type="text/javascript">
var _vis_opt_revenue = 0;
//zero should be replaced by the actual revenue figure
</script>
I'm using a Wufoo form and made it so that I can add a URL parameter that totals up their order, so for example if their order has a total of $280 they'll be sent to:
http://mysite.com/thank-you?total=280
I'm wondering: how can I make it so that URL parameter is inserted into the Javascript tracking code from Visual Website Optimizer?
Thanks in advance.
Function taken from here
Add this
function getParameterByName(name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.search);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
Then change
var _vis_opt_revenue = 0;
to
var _vis_opt_revenue = getParameterByName("total");
Try this
var _vis_opt_revenue =
'$' + window.location.pathname.split('?')[1].split('=')[1];

How can I get URL parameter in a jquery ready function?

I have the following:
$(document).ready(function() {
window.location.href='http://target.SchoolID/set?text=';
});
So if someone comes to a page with the above mentioned code using a url like:
Somepage.php?id=abc123
I want the text variable in the ready function to read: abc123
Any ideas?
you don't need jQuery. you can do this with plain JS
function getParameterByName( name ) //courtesy Artem
{
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, " "));
}
and then you can just get the querystring value like this:
alert(getParameterByName('text'));
also look here for other ways and plugins: How can I get query string values in JavaScript?
check out the answer to this questions:
How can I get query string values in JavaScript?
that'll give you the value of the id parameter from the query string.
then you can just do something like:
$(document).ready(function() {
var theId = getParameterByName( id)
var newPath = 'http://target.SchoolID/set?text=' + theId
window.location.href=newPath;
});
To help people coming after me that want to have multiple variables and to enhance Moin's answer here is the code for multiple variables:
function getParameterByName( name ) //courtesy Artem
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
{
if ((results[1].indexOf('?'))>0)
return decodeURIComponent(results[1].substring(0,results[1].indexOf('?')).replace(/\+/g, " "));
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
}

url parameter to save is in JavaScript

I would like to insert a parameter value found the url into my javascript code.. So basically i have the following url www.rene-zamm.com/mp-dthanks.asp?gglid=123123123
Now i have the following javascript code in the same page and i want that number in the url to be visible also in the javascript code below:
var gwoTracker=_gat._getTracker("UA-1639156-3");
gwoTracker._trackPageview("/**NUMBER OF URL HERE**/goal");
Can someone help me please?
You need to parse the querystring. Javascript has no inbuilt way of doing this easily, but there are some nice functions you can use:
function gup( 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 results[1];
}
http://www.netlobo.com/url_query_string_javascript.html
Then in your example you could do:
var queryData = gup("gglid");
var gwoTracker=_gat._getTracker("UA-1639156-3"); gwoTracker._trackPageview("/" + queryData + "/goal");

Categories

Resources