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';
Related
Hello everyone ı want to control my url address in my code ı want to redirect when url its true
if (data.is_okey && **URL** == true) {
window.location.replace('https://google.com.tr');
}
The current address in your browser is available with the variable window.location.href
So you should write:
if (data.is_okey && window.location.href == "**SOME URL YOU'D LIKE TO BE TRUE**") {
window.location.replace('https://google.com.tr');
}
I see that you want to check if a url is equal to a unique/decided one?
For example;
let checkUrl = "/login"
let url = window.location.pathname;
if(data.is_okey && url != checkUrl) {
window.location.replace("/login")
}
If you want sth like this. Please try it or explain with more details.
I have this link http://localhost:3007/sellers-toolkit/, but the right page is only the one without a trailing slash. So I would like a redirection to http://localhost:3007/sellers-toolkit.
I tried to write this part in JavaScript, but it just makes the page reload and nothing happens. What is the right way to achieve this behavior?
var url = "http://localhost:3007/sellers-toolkit/";
if (url) {
url = "http://localhost:3007/sellers-toolkit";
window.location.href = url;
}
my solution for this issue is that i was trying to do that on the javascript side but i found that i have to do this on my node server side.
i used this code:
if (req.url == '/sellers-toolkit' || req.url == '/sellers-toolkit/' ) {
category = 'Selling Privately';
}
Try like below:
var url = "http://localhost:3007/sellers-toolkit/";
var spe_char_last =url.substr(url.length - 1);
if(spe_char_last == '/')
{
url = url.slice(0, -1);
if (url) {
url = "http://localhost:3007/sellers-toolkit";
window.location.href = url;
}
}
function setInit(){
iu.init({
saveFile : 'store/saveImageFile'
,CallBackOnSave : function (s){
console.log("redirect");
window.location.replace("store/details");
}
});
}
iu is an image upload module which has no issues. In fact I can see the console log of redirect.
problem is, the same page gets reloaded, instead of going to the url in replace.
tried other ways like href as well, sadly the same result.
Just use:
window.location.href = "store/details";
Maybe try:
parent.window.location.href = "store/details";
You can use by this way,
Syntax
window.location.href = window.location.host + relativePath
window.location.href = relativePath
example
window.location.href = "store/details";
window.location.href = window.location.host + 'store/details';
I am trying to write a function like this:
if ( document.url!= 'search.php') {
window.location = 'search.php'
}
This is not working; it seems I may need to check if URL string contains 'search.php' - or is there another way I can do this check?
It sounds like you're searching for indexOf. Note that it's not document.url, but document.URL:
if (document.URL.indexOf('search.php') == -1) {
window.location = 'search.php';
}
Note: This will also match the following:
http://www.domain.com/search.php
http://www.domain.com/index.php?page=search.php&test
http://www.domain.com/search.php/non-search.php
http://www.domain.com/non-search.php#search.php
Extra note: Why are you doing this? If people have javascript disabled, they will never get forwarded. Maybe you're searching for the 301 or 302 header?
I always use this.
if (window.location.href != 'search.php') {//get the current url
window.location.href = 'search.php' //redirect
}
Try any of these
- alert(document.URL)
- alert(Window.location.href)
- alert(document.location.href)
- alert(window.location.pathname)
for creating dropdown with links -:Put this in the success part of your ajax
var data=JSON.parse(xmlhttp.responseText);
var alldata;
for(var i=0;i< data.length;i++)
{
alldata += "<a id='some_id' onclick='myFunction(this.id);' href='whatever#' class='link'>"+data[i][0]+"</a><hr>";
}
//data[i][0]-->this depends on your json text.
document.getElementById('your_input_field_id or div_id').innerHTML = alldata;
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;
}
}