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;
}
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 trying to change the URL in the address bar using javascript.
So if the user access the page using
www.example.com/ajax/project8.html
Url should be changed automatically to
www.examp.com/#cbp=ajax/project8.html
shouldn't be any harder than this:
window.location = "http://whatever.you.want.com"
UPDATE
So you want your site to redirect to another page when the url is www.example.com/ajax/project.aspx?id=whatever and id=xxx could be any id.
To achieve that you need a function that returns the query string parameter value eg:id=whatever
Then check if the current url needs to be redirected to another page. If this is the case then redirect to new url with same parameter value.
/*
function that returns a query string parameter value
this function works with many parameters
Eg: www.example.com/#cbp=ajax/project.aspx?myParam=hello&id=1283&otherParam=234
to get the param value just give it the parameters name
getQueryStringValue("id") returns : 1283
getQueryStringValue("myParam") returns : "hello"
*/
function getQueryStringValue( 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];
}
//current url
var currentUrl = location.href;
//check if current url contains www.example.com/ajax/project.aspx
if (currentUrl.indexOf("www.example.com/ajax/project.aspx") != -1 ){
//new url for redirection
var newUrl = "www.example.com/#cbp=ajax/project.aspx?id=" + getQueryStringValue( "id" );
//redirect to new page
location.href = newUrl;
}
Try this code
if (window.location.href == 'www.example.com/ajax/project8.html') {
window.location = 'www.examp.com/#cbp=ajax/project8.html';
}
you can set all things like
window.location.href = "www.examp.com/#cbp=ajax/project8.html"
for more details how you will manage all url parameter then please see
JavaScript and jQuery url managment
window.location.href = "#cbp=ajax/project8.html";
you can change the value written after # to any location , div id etc.
e.g
window.location.href = "#myDivID";
<meta http-equiv="refresh" content="0; url=http://example.com/" />
Note: please put on header
or
<script type="text/javascript">
window.location.assign("http://www.example.com")
</script>
Let's say I have a form located at:
http://www.mysite.com/some/place/some/where/index.php
And I have this form:
<form action="submit.php"> ... </form>
If I do this:
$("form").attr("action");
I will get:
submit.php
What is a foolproof way to get the action from the form, so that it includes the entire URL:
http://www.mysite.com/some/place/some/where/submit.php
?
Try
$("form").prop("action");
or (assuming your form is in the same folder as the page and the page URL ends with /something)
var loc = location.href;
var page = loc.substring(0,loc.lastIndexOf("/")+1)+
$("form").attr("action");
Handle query strings and hashes especially for Quentin:
Live Demo
var loc = location.origin + location.pathname;
var action = $("form").attr("action").split("/").pop();
var page = loc.substring(0,loc.lastIndexOf("/"))+"/"+action;
var el = document.createElement('a');
el.href = $("form").prop("action");
alert(el.href);
Chrome adjusts the href to give the absolute url, not sure with other browsers
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);
});
});
I have a textbox where the user can enter URL and button named Check link. On clicking button, a new window should be opened with the URL entered by the user. If i enter URL as "http://google.com" then window.open('http://google.com'); is working fine but if i enter "www.google.com" then it is appending to the current window url (http://localhost:1234www.google.com) which is not a valid url. How to make this work?
Thanks in advance
Check & prepend
if (!/^(http:|https:)/i.test(url))
url = "http://" + url;
try this:
function f(url) {
if (url.indexOf("http://") == -1) {
url = "http://" + url;
}
window.open(url);
}
If your question is "How do I test if a string starts with 'http://' and if not prepend it?" then something like this:
var url = "somestring.com"; // as set from your textbox
if (0 != url.toLowerCase().indexOf("http"))
url = "http://" + url;
It would be wise to trim leading and trailing spaces before doing this test, but I'll leave that as an exercise for the reader...
Add a onclick button function and handle the text in that.
function checkLink() {
var url = document.getElementById("textbox-id").value;
if(url.substr(0,7) != "http://") {
alert("Invalid URL! Please make it like http://myurl.com");
return false;
}
}