Removing \&quote; from the beginning of an url - javascript

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
});
});

Related

Current URL hashchange event append new hash to target URL and link out to it

I have a page ex: test123.com/go#1 on that page if the hash changes ex: test123.com/go#2 I'd like to append the new hash value to a target URL and link out to it: test123.com/gopage2#2
Here's the code I've tried so far:
var url = window.location.href;
var res = url.split("#");
var hash = "#" + res[1];
$(window).on('hashchange', function() {
window.location = 'test123.com/gopage2' + hash;
});
When the hash changes nothing happens... Any ideas?
Assuming you meant to update url, res, and hash in the event handler, and use the hash in the new URL, try this:
$(window).on('hashchange', function() {
var url = window.location.href;
var res = url.split("#");
var hash = "#" + res[1];
window.location = 'test123.com/gopage' + res[1] + hash;
});

Replaced %20 in URL with a +

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());

change part of the url text and reload page

Is there a way to make an a tag link that includes part of the current url?
I would use it to change language but stay on the current page!
If I am on:
www.mysite.com/contact
and I press on:
<a>FR</a>
the generated link would be:
www.mystie.com/fr/contact
Kind regards
Try this:
HTML:
FR
Now get the url from javascript
$("#myLink").click(function(e){
e.preventDefault();
var url = document.url; // get current page url
url = url.substr(0, url.indexOf('/')); // get substring from url
url += "/" + $(this).attr("href"); // add href of a tag to url
window.location.href = url; // locate the page to url
});
you can try with some like
function changeLng(lang) {
// remove language '/fr/' or '/es/' if exists
var path = window.location.pathname.replace(/\/[a-z]{2}/, '');
// reload the page same url with the lang prefix
window.location.href = '/' + lang + path;
}
the link can be
FR
ES
EN
this converts any url to lang specific, sample
/contact to /fr/contact
/about to /fr/about
update
for remove the lang part is simple
function removeLng() {
// remove language
var path = window.location.pathname.replace(/\/[a-z]{2}/, '');
window.location.href = path;
}

Change the folder name of URL with a text link?

I have a site with two languages contents, both have the same file names but are stored in different folders: /EN/ and /ZH/.
I would like to have a text link which allow it to be clicked and change the folder name of the URL.
like by clicking the text link "ZH", it will change the URL as:
from => "http://example.com/GroupA/EN/index.asp"
to => /ZH/index.asp"
I have search around and found a script as below:
Script:
$(function() {
$(".flag").click(function(e) {
e.preventDefault();
var to = $(this).attr("href").substring(1); //removes the hash value # (#en will become 'en')
var from = jQuery.url.segment(-2);
var url = from.replace('/' + from + '/', '/' + to + '/');
document.location = url;
});
});
Body:
<a id="flags" href="#en" class="flag">English</a>
However, when I tried the script above, it only add "#en" to the end of my URL, like
http://example.com/GroupA/EN/index.asp#en
$(function() {
$(".flag").click(function(e) {
e.preventDefault();
var to = $(this).attr("href").substring(1); //removes the hash value # (#en will become 'en')
var url = window.location.hostname + "/GroupA/"+to.toUpperCase()+"/index.asp"
window.location.assign(url);
});
});

How do I check href attribute and if needed add full url address?

I want to check the href attribute and if it does not contain the full path, I want to replace it with full URL? Should I use JavaScript which does that but don't work on IE 8?
My JS:
fullUrl = $(this).filter('[href^='+document.location.protocol+']').attr('href')
? true : false;
url = fullUrl ?
$(this).filter('[href^='+document.location.protocol+']').attr('href')
: document.location.protocol + '//' + document.domain + $(this).attr('href');
I need to check if href contain full url or not:
href: "/path/other.html" (part)
href:"http://domain.com/path/other.html" (full url)
And then if i have part url href, i must add domain and recreate href to full url!
The full url is available via the .href property.
Typically there's no need to set it to the attribute, but if you want to, you could do this:
$('a[href]').attr('href', function() { return this.href; });
This finds all <a> elements that have an href attribute, and updates its href using the attr()[docs] method by passing a function as the second parameter, which returns the value of the .href property.
If you just wanted a list of them, you can use the map()[docs] method.
var hrefs = $('a[href]').map(function() { return this.href; }).get();
Example: http://jsfiddle.net/EDhhD/
EDIT:
If you want to explicitly check that the path isn't already the full path, just add an if statement to my first example.
$('a[href]').attr('href', function(i,hrf) {
if( hrf.indexOf( 'http' ) !== 0 ) return this.href;
});
I guess you want this:
$(this).attr('href', function(i, v) {
if ( v.indexOf('http:') === -1 ) {
v = document.location.protocol + '//' + document.domain + '/' + v;
}
return v;
});
Live demo: http://jsfiddle.net/simevidas/bwmQ5/
I don't think you can get a meaningful result of calling attr on multiple items. You should do your check/replacement in a loop, one item at a time:
$(this).filter('[href^='+document.location.protocol+']').each(function(){
var fullUrl = $(this).attr('href') ? true : false;
var url = fullUrl ? $(this).attr('href') : document.location.protocol + '//' + document.domain + $(this).attr('href');
alert(url);
});
This solution will ensure all anchors on your page have the protocol in the href:
$(document).ready(function()
{
var str_len = document.location.protocol.length;
$('a').each(function()
{
if($(this).attr('href').substring(0,str_len) != document.location.protocol)
{
$(this).attr('href',document.location.protocol + '//' + $(this).attr('href'));
}
});
});
http://jsfiddle.net/3XQXL/

Categories

Resources