I'm looking to get a URL from a query. (The query is "?url=urlgoeshere" [where "urlgoeshere" is the URL to redirect to]) and redirect to that URL. For some reason when I run the script it goes to "http://mydomain.ext/urlgoeshere" (where "urlgoeshere" is the URL to redirect to)
Any ideas on how I could fix this issue?
This is my current code:
var loc = window.location.href;
var url = window.location.search.split("?url=");
var countdown=5
var currentsecond=document.rd.box.value=countdown+1
function redirect(){
if (currentsecond!=1){
currentsecond-=1
document.rd.box.value=currentsecond
} else {
open(url);
return
}
setTimeout("redirect()",1000)
}
redirect()
<form name="rd">
<center>
<br><br><br><br>
<font face="Arial"><b>You will be redirected in<br><br>
<input type="text" size="3" name="box">
</form>
seconds</b></font>
</center>
After splitting your url. Your url will be an array of two entries.
url[0] will be your current page url.
url[1] will be the url you want to redirect.
So in your code use the following:
open(url[1])
Related
(1)
My example Current URL along with Parameters is ----
www.example.com?fname=John&femail=john123#example.com
(2)
Through html / JavaScript
I want to check Current URL Parameter whether it contains any data in
fname
(3a)
Next, If there is No URL Parameter present then Redirect to "www.example.com/error-page"
or
(3b)
If the parameter fname have some data (No need for any Validation of data) meaning the parameter fname is not empty then should Continue with the execution of Current Page.
I tried the following successfully :
<!doctype html>
<html>
<head>
<body>
<div>
<p id ="dd"></p>
</div>
<meta charset="UTF-8"/>
<script type="text/javascript">
var iid=document.getElementById("dd");
var getURL=window.location.href;
var theString = window.location.href;
var theWord = "fname";
var theWordExp = /fname/g;
if (theString.search(theWordExp) == -1) { window.location.href=
('www.example.com/error-page'); };
</script>
</body>
</head>
</html>
Explanation:
"I want to check Current URL Parameter whether it contains any data in fname"
The getQueryParam function is explained here
How to get "GET" request parameters in JavaScript?
basically it's almost the same as your approach using the location href to parse the params
"If there is No URL Parameter present then Redirect to" else continue, for this you'll only need to wrap it inside a div, if the conditional is false (found param) then it'll just not run the statement inside if block (the one that will redirect user to error page)
Note that you have many other option to implement, check with the compatibility of browser, behaviour of redirection can also be changed to replace the last history page so user cannot go back to the previous URL that throw the error using window.location.replace method
const getQueryParam = (name) => {
if (name = (new RegExp('[?&]' + encodeURIComponent(name) + '=([^&]*)')).exec(location.search))
return decodeURIComponent(name[1]);
}
let fnameParam = getQueryParam("fname");
if (!fnameParam) {
window.location = "http://www.example.com/error-page";
};
<!doctype html>
<html>
<head>
<body>
<div>
<p id="dd"></p>
</div>
</body>
</head>
</html>
Hello I am super new to building websites. Please excuse my lacking terminology!!
I have a website that has Wildcard sub-domains. It is using this script to pull the wildcard sub-domains usernames.
<p id="dist-info"></p>
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
return;
var get_data_url = 'https://backoffice.WEBSITE.com/api/v2/public/users/{username}';
$.getJSON(get_data_url, function( data ) {
var dist_info = "<p>"+data.response['first-name']+"</p>" +
"<p>"+data.response['last-name']+"</p>" +
"<p>"+data.response['distributor-id']+" "+data.response.email+"</p>" +
"<p>"+data.response['image-url']+"</p>" +
"<p>Phone: "+data.response.phone+"</p>";
$('#dist-info').html(dist_info);
});
});
</script>
Now I need to make a URL that will parse the username/user id out of the Wildcard Subdomain page. What code do I need to use?
For example
The URL is
USERNAME.WEBSITE.com/page/subpage/
I need to make this URL
backoffice.WEBSITE.com/page?sponsor-id=USERNAME
What do I need to do so that the username from the first page is parsed out and applied to the link of the second URL
You want to modify this JavaScript so it uses the subdomain instead of {username}?
$(document).ready(function() {
var get_data_url = 'https://backoffice.WEBSITE.com/api/v2/public/users/';
var hostname_parts = location.hostname.split('.');
var username = hostname_parts.shift();
// add username to the data URL
get_data_url += username;
// add username to some link on the website
var $aTag = $('#id-of-the-link');
var link_url = $aTag.attr('href');
link_url += username;
// set the new href attribute
$aTag.attr('href', link_url);
location.hostname gives you USERNAME.WEBSITE.com, split() splits it into parts separated by a dot. shift() takes the 1st element of this array (you could also use hostname_parts[0]) and with += you concatenate it to the URL.
The second example shows how to add the username at the end of a link like
click
Edit: added example for changing a href attribute
I want to know from which url a form is send. I've tried this, but the result is 'orgUrl' instead of the url where the form is send from.
Anyone an idea what's wrong with this code?
<input type="hidden" name="url" value=orgUrl>
<script>
$(document).ready(function(){
var orgUrl = window.location.href;
});
</script>
You're trying to mix javascript with normal html which is not possible.
So you need to set the value also by javascript after getting the current url like this
<input type="hidden" id="url" name="url" value>
<script>
$(document).ready(function(){
var orgUrl = window.location.href;
$("#url").val(orgUrl );
});
</script>
i have an url like this
/users/?i=0&p=90
how can i remove in js the part from
? to 90
can any one show me some code?
EDIT
i mean doing this with window.location.href (so in browser url bar directly)
i tryed
function removeParamsFromBrowserURL(){
document.location.href = transform(document.location.href.split("?")[0]);
return document.location.href;
}
also i would like to not make redirect, so just clean the url from ? to end
function removeParamsFromBrowserURL(){
return window.location.href.replace(/\?.*/,'');
}
If you only want the /users/ portion:
var newLoc = location.href.replace( /\?.+$/, '' );
You could also split the string, and return the first portion:
var newLoc = location.href.split("?")[0];
Or you could match everything up to the question mark:
if ( matches = location.href.match( /^(.+)\?/ ) ) {
alert( matches[1] );
}
One way is leftside = whole.split('?')[0], assuming there's no ? in the desired left side
http://jsfiddle.net/wjG5U/1/
This will remove ?... from the url and automatically reload the browser to the stripped url (can't get it to work in JSFiddle) I have the code below in a file, and put some ?a=b content manually then clicked the button.
<html>
<head>
<script type="text/javascript">
function strip() {
whole=document.location.href;
leftside = whole.split('?')[0];
document.location.href=leftside;
}
</script>
</head>
<body>
<button onclick="strip()">Click</button>
</body>
</html>
If you only want the /users/ portion, then you could just substring it:
var url = users/?i=0&p=90;
var urlWithNoParams = url.substring(0, url.indexOf('?') - 1);
That extracts the string from index 0 to the character just before the '?' character.
I had problems with #page back and forth referrals sticking in the url no matter which url redirect I used. This solved everything.
I used the script like this:
<script type="text/javascript">
function strip() {
whole=document.location.href;
leftside = whole.split('#')[0];
document.location.href=leftside;
}
</script>
<a onclick="strip()" href="http://[mysite]/hent.asp" >Click here</a>
I am using Google contacts JavaScript API. I am trying to add contacts to the gmail account of the authenticated users using the code given in the
http://code.google.com/apis/contacts/docs/1.0/developers_guide_js.html#Interactive_Samples.
I am able to login and logout, but I try to create a new contact my Chrome is given an error. I have hosted the JavaScript and html file in the Amazon s3 bucket and also image.
Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL https://s3.amazonaws.com/googlecontacts/google_contacts.html. Domains, protocols and ports must match.
And contacts are not created.
HTML file
<!DOCTYPE HTML>
<head> <title> Google contacts </title>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript" src="auth.js" > </script>
</head>
<body>
<h1> Google contacts </h1>
<img src="rss_icon.jpg" width="100" height="100" />
<input type="button" value="login" onclick="logMeIn()" />
<input type="button" value="logout" onclick="logMeOut()" />
<input type="button" value="createContact" onclick="createContact()" />
</body>
</html>
javascript file
google.load( 'gdata', '1.x' );
var contactsService;
function setupContactsService() {
contactsService = new google.gdata.contacts.ContactsService('GoogleInc-jsguide-1.0');
}
function logMeIn() {
var scope = 'https://www.google.com/m8/feeds';
var token = google.accounts.user.login(scope);
}
function logMeOut() {
google.accounts.user.logout();
}
function createContact() {
/*
* Create a contact entry
*/
// Create the contacts service object
var contactsService =
new google.gdata.contacts.ContactsService('GoogleInc-jsguide-1.0');
// The feed URI that is used to create a contact entry
var feedUri = 'http://www.google.com/m8/feeds/contacts/default/full';
// Create an instance of ContactEntry
var entry = new google.gdata.contacts.ContactEntry();
// Set the name of the contact
entry.setTitle(google.gdata.Text.create('JS-Client: Create Contact'));
// Set the content of the contact
entry.setContent(google.gdata.Text.create('content info here'));
// Create an email instance
var email = new google.gdata.Email();
email.setAddress('JS-Client#domain.com');
email.setPrimary(true);
// Designate this email as the "home" email
email.setRel(google.gdata.Email.REL_HOME);
// Add the email instance
entry.setEmailAddresses([email]);
// The callback method that will be called after a successful insertion from insertEntry()
var callback = function(result) {
PRINT('contact entry created!');
}
// Error handler will be invoked if there is an error from insertEntry()
var handleError = function(error) {
document.getWriter='error';
}
// Submit the request using the contacts service object
contactsService.insertEntry(feedUri, entry, callback,
handleError, google.gdata.contacts.ContactEntry);
}
The problem was I was access https server from http server, so the protocol mis matched just changed the feedURi http://www.google.com/m8/feeds/contacts/default/full'; to
https://www.google.com/m8/feeds/contacts/default/full';
Have you tried putting google.load( 'gdata', '1.x' ); in the html file?
It worked for me.