Replaced %20 in URL with a + - javascript

I was wondering is it possible to replace the %20 to a + whenever I click the button to add the textbox value to the url.
$(function () {
$('#btn').click(function() {
var url = "www.urlname.com/results.html?value=";
url += $('#textbox').val();
window.location = url;
});
});
Thanks

Yep with a regex for all occurrences $('#textbox').val().replace(/%20/g, '+');

I haven't tested it but this should work. You want to replace the spaces in the value. They're not encoded to the entity at this point.
$(function () {
$('#btn').click(function() {
var url = "www.urlname.com/results.html?value=";
var textboxValue = $('#textbox').val();
textboxValue = textboxValue.replace(/ /g, "+");
url += textboxValue;
window.location = url;
});
});

Have you tried this?
$(function () {
$('#btn').click(function() {
var url = "www.urlname.com/results.html?value=";
url += $('#textbox').val();
window.location = url.replace(new RegExp(' ','g'),'+');
});
});

When you're creating URL parameters dynamically, you shouldn't use ad hoc substitutions to encode them. Javascript provides the encodeURIComponent function to perform all required encodings. So you should write:
url += encodeURIComponent($('#textbox').val());

Related

Removing \&quote; from the beginning of an url

I want to remove "&quote; from the beginning of an url like for instance when a url has something like this href=""https://www.google.com" how do i get rid of the &quote; from the beginning of the url using javascript.
I tried something like this,
$(document).ready(function(){
$('a[href^="\"https://"]').each(function(){
var oldUrl = $(this).attr("href"); // Get current url
var newUrl = oldUrl.replace("\"https://", "https://"); // Create new url
$(this).attr("href", newUrl); // Set herf value
});
});
Google
Gmail
The working code :
$(document).ready(function(){
$("a").each(function() {
//console.log($(this).attr("href"));
var oldUrl = $(this).attr("href"); // Get current url
var newUrl = oldUrl.replace('\\"', ' ');
//console.log($(this).attr("href").replace('\\"', ' '));
$(this).attr("href", newUrl); // Set herf value
});
});

Redirect from master page to its content pages using jquery not working

I called this method on keyUp event of enter key
function PerformSearch(search_key) {
var url = "<%=ResolveUrl("/Test/TestPage.aspx?type=search&search_key=")%>" + search_key;
//GoToLocation(url);
//window.open(url);
//$(location).attr("href", url);
//window.location.replace(url);
window.location.href = url;
}
use like this
window.location = url;
For your reference read this
You have string concatenation errors in your url.
This:
var url = "<%=ResolveUrl("/Test/TestPage.aspx?type=search&search_key=")%>" + search_key;
Should be this:
var url = "<%=ResolveUrl('/Test/TestPage.aspx?type=search&search_key=')%>" + search_key;

How can I put something at the end of the URL using jQuery upon clicking a dropdown? [duplicate]

Is there is a way how to add hash # to my URL without redirect?
window.location.hash = 'something';
That is just plain JavaScript.
Your comment...
Hi, what I really need is to add only the hash... something like this: window.location.hash = '#'; but in this way nothing is added.
Try this...
window.location = '#';
Also, don't forget about the window.location.replace() method.
For straight HTML, with no JavaScript required:
Add '#something' to URL
Or, to take your question more literally, to just add '#' to the URL:
Add '#' to URL
window.location.hash = 'whatever';
Try this
var URL = "scratch.mit.edu/projects";
var mainURL = window.location.pathname;
if (mainURL == URL) {
mainURL += ( mainURL.match( /[\?]/g ) ? '&' : '#' ) + '_bypasssharerestrictions_';
console.log(mainURL)
}

Append parameter to URL address not work in multiple function (second function not change)

I try to change url in my website with javascript. First function (degis) ischanging url. after run degis function, I want to run listele function to change url adress. (add parameter) but second function is not changinglink. I can get alert message from second code. What should I do? thanks.
<script type="text/javascript">
var q=0;
function degis(src){
q= src.value;
window.location = '/cgi-bin/koha/tools/dev_yetki.pl?q='+q;
alert("1");
}
function listele(){
var x=document.getElementById("yeniden");
var y= x.value;
alert(y);
var url = window.location.href;
if (url.indexOf('?') > -1){
url += '&y='+y
}else{
url += '?q='+q
}
window.location.href = url;
}
Try to use window.location.replace(url) or window.location.reload(url) instead of window.location.href = url;
Missing q in url += '?q='+q statement in the second function. Try this:
function listele(src){
q= src.value;
var x=document.getElementById("yeniden");
var y= x.value;
var url = window.location.href;
if (url.indexOf('?') > -1){
url += '&y='+y
}else{
url += '?q='+q
}
window.location = url;
}
You're not calling degis. Add degis() wherever you want the url to change.

How to get part of URL current?

My Url looks like: http://google.com/stackoverflow/post/1
Now, I want to get just part of URL: http://google.com/stackoverflow/ and add it to code:
Post . How to do it ? Thanks !
Plz try this:
var url = "http://google.com/stackoverflow/post/1";
var partOfUrl = url.split('post')[0];
Thanks.
UPDATED:
Using JavaScript:
var url = "http://google.com/stackoverflow/post/1"; // or var url = window.location;
var matches = url.match(/^http\:\/\/([\w\.]+)\/(\w+)\/.+/);
if( matches ){
var newURL = "http://" + matches[1] + "/" + matches[2] + "/";
alert( newURL );
}
document.getElementById('post_link').href = newURL;
HTML:
<a id="post_link">post</a>
See JSFiddle
use location Object
location.href
location.pathname

Categories

Resources