Window.Open causing issue when URL doesnot contain http - javascript

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

Related

redirect a page from URL jquery

I want to redirect from a url. It works when I use #!, but I want to work with a parameter other than this.
//works
url = 'localhost/#!/about'
if (document.location.hash.replace(/^#?!\//, '') != '') {
loadResource(true);
}
//doesn't work
url = 'localhost/about'
if(location.pathname.replace(/^\/([^\/]*).*$/, '$1') != ''){
loadResource(true);
}
Why? Is there a way to not use #!?
The code above doesn't show any code for redirection. What does loadResource do?
Anyways, redirect in javascript is done like this:
// Syntax: document.location.href = url;
document.location.href = 'http://localhost/about';

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 URL using javascript Jquery

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>

Loop on back button after re-direction with querystring

The problem:
I need to start with a URL with a query string containing a URL of a second page - http://www.firstURL.com/?http://www.secondURL.com. On the target page of the first URL, the query string is parsed to extract the second URL and the browser is re-directed to the second URL. This is done on $(document).ready so that it's automatic. This all works fine, but of course falls in a hole if the user hits the back button on the second URL. Here's the basic code:
$(document).ready(function() {
var s = location.search;
if(s != '') {
var split = s.split('?');
var loc = split[1].replace('?', '');
location.href = '' + loc + '';
} else {
//do something else on the target page..
}
});
I've tried creating a conditional case where, if the referrer is the 2nd URL (loc in the code above), the re-direction doesn't execute, but it seems that in the case of a re-direction, the back button doesn't return the referrer.
I have to do all this client side - I have no access to the server.
Is there some way to prevent the re-direction triggering on a back button click? Thanks.
Once you hit the second page, set a cookie in your browser indicating that the second page has been visited.
In the first page, before doing the redirection always check whether the cookie is not present.
Instructions on setting a cookie:
<script type="text/javascript">
document.cookie="secondpagevisited=43yj0u3jt;path=/"; //execute this line in the head of second page.
</script>
In first page, check for cookie presence:
<script type="text/javascript">
if(document.cookie.indexOf("secondpagevisited=43yj0u3jt")==-1){
/*do redirection here*/
}
</script>
EDIT: Assuming you control only the first page and not the second page, try this:
<script type="text/javascript">
if(document.cookie.indexOf("secondpagevisited=43yj0u3jt")==-1){
document.cookie="secondpagevisited=43yj0u3jt;path=/";
/*do redirection here*/
}
</script>
I gave Ashish the point for putting me on the right track, but this is my solution which goes one step further:
var s = location.search;
if(s != '') {
var split = s.split('?');
var loc = split[1].replace('?', '');
if (document.cookie.indexOf('redirected=' + loc + '') == -1) {
document.cookie = 'redirected=' + loc + '';
location.href = '' + loc + '';
} else {
var url = location.href.replace('' + s + '', '');
document.cookie = 'redirected=; expires=Thu, 01 Jan 1970 00:00:00 GMT';
history.pushState(null, null, '' + url + '');
}
If the cookie is there, the re-direction doesn't occur, the cookie is removed (in case the user returns to the site that had the original link and clicks it again), and the URL is tidied up by removing the query string.
Thanks for the guidance.

How to set a query string value in javascript then reload a page

How do I add a query string value to a URL, then refresh that page?
The current url looks like: /foo/bar
I want to reload as /foo/bar?tabIndex=4
function onSomethingComplete() {
window.location.search = "tabIndex=4";
window.location.reload();
}
Thanks!
Should be able to do something like this:
function onSomethingComplete() {
var url = window.location
url += "?tabIndex=4"
window.location = url
}

Categories

Resources