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);
});
});
Related
I want to remove ""e; 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 "e; 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
});
});
I'm still new to Node.js, but I'll try to explain my problem as good as I can.
So I'm working on a movie site to practice my node.js/express skills a bit and I use the following elements (img) on basically every page of my website:
Header with stats and search input and a navigation bar (will be reused on every page)
The follow JS-code are two examples of actions on the client side that I use to navigate to other web pages, this then activates GET on the client side.
$(function () {
$('button').click(function (event) {
event.preventDefault()
// the value people enter in the search box
var search = $('.searchInput').val();
//replace spaces with _
var res = search.replace(/\s/g, "_");
//build the URL
var link = window.location.protocol + '//' + window.location.host + '/search/' + res;
// redirect to trigger GET in indexjs with specific search value
window.location.replace(link);
});
$('.lists').click(function (event) {
var link = window.location.protocol + '//' + window.location.host + '/lists/topAll';
window.location.replace(link);
})
});
I want this to be the same code on every page. I could type the same code every time, but that would be a waste of time.For HTML and CSS I am able to use templates (HTML) or import other CSS files to save time. Is there something similar for JS on the client side?
Put that code in a file, for example "navigator.js" and then load it in your html header in every page you want to use it
navigator.js:
$(function () {
$('button').click(function (event) {
event.preventDefault()
// the value people enter in the search box
var search = $('.searchInput').val();
//replace spaces with _
var res = search.replace(/\s/g, "_");
//build the URL
var link = window.location.protocol + '//' + window.location.host + '/search/' + res;
// redirect to trigger GET in indexjs with specific search value
window.location.replace(link);
});
$('.lists').click(function (event) {
var link = window.location.protocol + '//' + window.location.host + '/lists/topAll';
window.location.replace(link);
})
});
index.html
<script src="navigator.js"></script>
Finally i suggest you to assign an id to your button, for example "searchButton" instead only "button"
Hope this helps
I am trying to add a onclick function to a button outside of the <button> tag, in Javascript. Below is what I have at the moment, but the button doesn't links to that uri when clicked. How should I do this? Many thanks for your help in advance! I commented in the code as well.
For the button I have:
"<td><button"+" id="+foodList[i].Id +" onClick="+"\"doSomething(this.id)\""+" >"+"Get"+"</button></td></tr>"
So basically I assigned the "Get" button an id in a for loop.
function doSomething(id) { //the button's id
var item = document.getElementById("type").value; //get some value from elsewhere in the page
if (item == "food") {
var uri = "baseuri" + id;
document.getElementById(id).onclick = "location.href='" + uri + "';"//add an onclick to the button so that it will takes the user to that uri
} else {
var uri = "another baseuri" + id;
document.getElementById(id).onclick = "location.href='" + uri + "';"
}
Change this:
document.getElementById(id).onclick="location.href='"+uri+"';"
to something like this:
document.getElementById(id).onclick= function(){
location.href = "Wanted url here"; // location.href = location.href + "/currentpath/additional/params/here"
}
That way, when you click the button, you have attached a function to it, and it changes the url (redirects the page)
You must write it like this :
document.getElementById(id).onclick = function() {
location.href = uri;
}
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());
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;
}