I have an aspx page and a asp textbox control which has ajax autoCompleteExtender. I want the page to be redirected to another page according to the selected element from the autocomplete list. But when I use
window.location()
nothing is happening, just the same page is refreshed. Here is my javascript;
<script type="text/javascript">
function selectCity() {
var str = document.getElementById('<%= txtSearchForDestination.ClientID %>').value;
var array = str.split(",");
var city = array[0].replace(/^\s\s*/, '').replace(/\s\s*$/, '');
city = city.replace(/ /g, "+")
var country = array[1].replace(/^\s\s*/, '').replace(/\s\s*$/, '');
country = country.replace(/ /g, "+")
window.location.href("City.aspx?city=" + city + "-" + country);
}
</script>
The script is working, I tried it with like
alert("City.aspx?city=" + city + "-" + country)
there is no problem. But when I want to redirect to that page it is not working. I also tried
window.location("http://www.google.com")
it's not working as well.
What can be the problem ?
It's not a function, it's a property.
window.location.href = "City.aspx?city=" + city + "-" + country;
Have you tried:
window.location = 'City.aspx?city=' + city + '-' + country;
?
Related
I want to remove the "http" if it is put in the url part of the input link, before the data is sent.
this my input code look onclick=
<input style=" outline: none;" type="button" onclick="formatText ('link:url');" class="btn btn-yeni" value="link"/>
This my javascript code (the received data is sent to another file and replaced.)
<script type="text/javascript">
function formatText(tag) {
var Field = document.getElementById('entry_girdi');
var val = Field.value;
var selected_txt = val.substring(Field.selectionStart, Field.selectionEnd);
var before_txt = val.substring(0, Field.selectionStart);
var after_txt = val.substring(Field.selectionEnd, val.length);
Field.value += '(' + tag + ')';
}
</script>
what i want to do If the input value is "link: http: //example.com" I would like to change it and post it as "link: example.com".
Can you try in your url string :
var result = url.replace(/(\w+:|^)\/\//, '');
result variable will hold "link : example.com" in place of "link : http://example.com"
Use the replace() function to replace part of a string.
function formatText(tag) {
var Field = document.getElementById('entry_girdi');
Field.value = Field.value.replace("http://", "");
Field.value += '(' + tag + ')';
}
So I'm currently able to capture the GCLID value from visitors and pass this through to our forms just fine. My question is, using the same scripts (attached below), is it possible to also capture a utm value as well?
For example, if a visitor comes to our website from a Bing ad, the url will be something like www.example.com/?utm_source=bing&utm_medium=cpc
I need to be able to store the value of utm_source (bing) in a cookie, and pass this value to our forms.
Code that's working for me currently with the GCLID:
Store GCLID in cookie (before </body> tag):
<script type="text/javascript">
function setCookie(name, value, days){
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
var expires = "; expires=" + date.toGMTString();
document.cookie = name + "=" + value + expires + ";path=/";
}
function getParam(p){
var match = RegExp('[?&]' + p + '= ([^&]*)').exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));}
var gclid = getParam('gclid');
if(gclid){
var gclsrc = getParam('gclsrc');
if(!gclsrc || gclsrc.indexOf('aw') !== -1){
setCookie('gclid', gclid, 90);
}
}
</script>
Pass value to form (in header):
<script>
window.onload = function getGclid() {
document.getElementById("00N3100000H5IBe").value = (name = new
RegExp('(?:^|;\\s*)gclid=([^;]*)').exec(document.cookie)) ?
name.split(",")[1] : "";
}
// window.onload() may not be supported by all browsers.
// If you experience problems submitting the GCLID as a
// hidden field, consider using an alternate method to
// call this function on page load.
</script>
I am able to modify the current scripts to capture the utm value but then it won't capture the gclid value. So far I have been unable to do both.
Any help or direction would be much appreciated. Please let me know if I need to better explain things. Thanks!
The code below puts the GCLID code in the hidden field OR the UTM_Source.
I have this all in the body as you first need to set the cookie and then read it out.
Good luck!
NOTE: the 'Field7' is the ID of the hidden field on my form.You will have to change that to your form.
<script type="text/javascript">
function setCookie(name, value, days){
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
var expires = "; expires=" + date.toGMTString();
document.cookie = name + "=" + value + expires + ";path=/";
}
function getParam(p){
var match = RegExp('[?&]' + p + '=([^&]*)').exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
function readCookie(name) {
var n = name + "=";
var cookie = document.cookie.split(';');
for(var i=0;i < cookie.length;i++) {
var c = cookie[i];
while (c.charAt(0)==' '){c = c.substring(1,c.length);}
if (c.indexOf(n) == 0){return c.substring(n.length,c.length);}
}
return null;
}
// Define the variables
var gclid = getParam('gclid');
var utm_source = getParam('utm_source');
// Check if there is a GCLID first, then check if there is a utm_source
if(gclid){
alert(gclid);
setCookie('gclid', gclid, 90);
} else if(utm_source) {
alert(utm_source);
setCookie('utm_source', utm_source, 90);
}
</script>
<script type="text/javascript"> // Now set the hidden field on the form by looking for the correct cookie name
window.onload = function() {
if (gclid) {
document.getElementById('Field7').value = readCookie('gclid');
} else if (utm_source) {
document.getElementById('Field7').value = readCookie('utm_source');
}
}
</script>
fI'm developing a simple and small search in a Wordpress page using a $_GET variable in the url passed by javascript:
<script>
function pesquisar()
{
var pesquisar = document.getElementById('termo').value;
var caminho = document.URL+'&pesquisa='+pesquisar;
window.location = caminho;
}
</script>
<input type='text' id='termo'>
<input type='button' value='pesquisar' onclick='pesquisar()'>
So, the url to search is: MYURL/?page_id=51&pesquisa=test
Of course, the page_id is variable. If I search again, the URL is going to be: MYURL/?page_id=51&pesquisa=test&pesquisa=new, what is wrong.
How could I get just the MYURL/?page_id=51 using javascript? The window.location.pathname is not what I need.
Thanks.
Anything that searches naively will be vulnerable to problems like this: What if the URL is:
http://example.com/?pesquisa=test&page_id=51
You need to search for and remove the relevant query parameter:
var caminho = document.URL.replace(/([?&])pesquisa=[^&]+&?/,"$1")+"&pesquisa="+pesquisar;
Try this hopefully it should work
<script>
function pesquisar()
{
var pesquisar = document.getElementById('termo').value;
var caminho = location.protocol + '//' + location.host + location.pathname+'&pesquisa='+pesquisar;
window.location = caminho;
}
</script>
<input type='text' id='termo'>
<input type='button' value='pesquisar' onclick='pesquisar()'>
try this.
var a = document.URL;
var result = a.substr(0, a.indexOf('&'));
Resources:
Get current URL in web browser
how to grab substring before a specified character jquery or javascript
javascript:
<script type="text/javascript">
function get_query_param(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
window.onload = function() {
if (get_query_param("page_id") != null) {
alert(window.location.pathname + "?page_id=" + get_query_param('page_id'));
}
}
</script>
hope that helps.
I have these links :
Block 1 :
Adapters
Battery
After click on the link in Block1 , then I start to click on Block2:
Block 2 :
<img src ="/Content/Images/Top/searchbutton.png"/>
I can get the parameter value such as dep=56,cat=654 by using these jquery.
function getParameterByName(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 decodeURIComponent(results[1].replace(/\+/g, " "));
}
function SearchClick() {
var cur_url = window.location.href;
var depId = getParameterByName("dep");
var catId = getParameterByName("cat");
var searchStr = getParameterByName("search");
var url_add = "";
if (depId != "") {
url_add += "&dep=" + depId;
}
window.location = "/Products?tab=2"+ url_add;
}
But now I exchanged the link in Block 1 with
<a href='javascript:void(0);' dep='" + work.ID + "'>" + work.ProName + "</a>
//it works well
So how can I get the value of dep in Block 1 by using javascript or jquery when I click on the the Block 2 link?
Thanks so much for all your answers.
It would be best if you put an ID on the A tag, like so:
<a id="myLink" href='javascript:void(0);' dep='" + work.ID + "'>" + work.ProName + "</a>
then you could use the following JQuery to get the value of work ID:
$("#myLink").attr("dep")
or plain old Javascript like so (assuming you've still got that ID on the A tag):
document.getElementById("myLink").getAttribute("dep")
EDIT: I've put it in the click for you, here is all my code:
<a id="block1Link" href='javascript:void(0);' dep="56">Adapters</a>
<br />
<br />
Click
<script type="text/javascript">
function SearchClick() {
var department = $("#block1Link").attr("dep");
alert(department);
}
</script>
Or if you want to use Javascript swap the var department line with this line:
var department = document.getElementById("block1Link").getAttribute("dep");
how can I use the values from the code below:
<html:select property="state" >
<html:option value="0">Select State</html:option>
<html:optionsCollection name="InputForm" property="stateList" label="label" value="value" />
</html:select>
to populate a textbox with whatever the selected value is for the state with a javascript onchange event? for example if texas is the selected state I want Texas to be written in the textbox and if I change the value to Colorado I would like Colorado and Texas to both show in the textbox. I am currently getting an undefined value in the textbox. I am using the following javascript code:
function displayState(obj, state) {
var theId = obj.id.substring(obj.id.indexOf('_') + 1);
var text = document.getElementById('text' + '_' + theId);
var textVal = text.value;
var stateSelect = document.getElementById('stateCode' + '_' + theId);
var stateSelectStr = new String(stateSelect.value);
var stateSelectSplit = stateSelectStr.split(',');
var stateSelectValue = stateSelectSplit[0];
var stateSelectLabel = stateSelectSplit[1];
if (stateSelectValue == '51') {
stateSelectLabel = '';
}
if (stateSelectValue != '00') {
textVal = textVal != '' ? eval('text.value=\'' + textVal + ', '
+ stateSelectLabel + '\'')
: eval('text.value=\'' + stateSelectLabel + '\'');
}
}
First off, start using jQuery (or equivalent), it will make your life much easier. Second, why are you using eval? You already have the text element; just build the appropriate string and set the value. See here for an example.
But again, using something like jQuery makes this laughably easy--I can't recommend using a library for simple DOM manipulation like this.
See here for a comparison between raw JS and JQ.