I am a little bit confused here.
I have a url locahost/product-location/agro-product and want when a user clicks on a button on this page it takes the user to locahost/product/agro-product. After some research i figured out i could change the pathname this way
<script type="text/javascript">
function loadPage(){
var theURL = window.location.pathname;
return theURL.replace("/product-location/", "/product/");
}
</script>
The above works because if I add this alert(loadPage()); outside the function: it alerts the new URL path.
Now how do I write the code from here so when a user clicks the button it takes the user to the new URL?
You can use window.location.href:
function loadPage(){
var theURL = window.location.pathname;
var newURL = theURL.replace("/product-location/", "/product/");
//Set URL
window.location.href = newURL;
}
OK.got it. Just had to do a little rewriting
function loadPage(){
var theURL = window.location.href;
return window.location = theURL.replace("/product-location/", "/product/");
//Set URL
}
I'd recommend using window.open because you can choose what window it opens in (not to mention a variety of different options).
function loadPage(){
var newURL = window.location.pathname.replace("/product-location/", "/product/");
window.open(newURL);
//LOAD IN NEW WINDOW/TAB INSTEAD:
//window.open(newURL, "_blank");
}
Your button HTML would look like this:
<button onclick="loadPage();">Visit new page</button>
Related
$('.fbshare').on('click', function(){
var url = window.location.href;
window.open('https://www.facebook.com/sharer/sharer.php?u=' + url,
'facebook-share-dialog',
'width=800,height=600'
);
});
The above works fine and I want the same for twitter:
$('.twshare').on('click', function(){
var url = window.location.href;
window.open('https://twitter.com/share?url=' + url,
'width=800,height=600'
);
});
this opens a new tab instead a popup window
also on the new tab - inside the text box - the url has a space before
Per the comments, it looks like the second argument of window.open(), windowName was left out in your Twitter example. Try this:
$('.twshare').on('click', function () {
var url = window.location.href;
window.open('https://twitter.com/share?url=' + url,
'twitter-share-dialog',
'width=800,height=600'
);
});
I have used history.pushState() and now if the user refreshes the page then it is refreshing current URL which is not an original URL.
I tried detecting refresh page with a cookie, hidden filed but it is not working.
window.onload = function() {
document.cookie="PR=0";
var read_cookies = document.cookie;
var s = read_cookies;
s = s.substring(0, s.indexOf(';'));
if( s.includes("1"))
{
window.location.href = "https://www.google.com";
}
else{
document.cookie="PR=1";
}
loadURL();
};
function loadURL()
{
document.cookie="PR=1";
document.getElementById("visited").value="1";
var str="abc/b cd";
str=str.replace(/ /g, "-");
history.pushState({},"",str);
}
when user is refreshing the page I need original URL on that time.
This might be helpful. But you need control over the pushed url.
// this goes to your 'original' url
window.addEventListener('beforeunload', function (event) {
sessionStorage.setItem('lastPage', window.location.href)
}
// page with your pushed url
if (sessionStorage.getItem('lastPage') === 'PAGE_YOU_DONT_WANT_TO_BE_REACHABLE_DIRECTLY') {
window.location = 'PAGE_YOU_WANT'
}
I'm interested what the use case for this is. As far as my knowledge goes you can't suppress the refresh event completely.
Link to my in codepen: codepen.io/neel111/pen/dRBQNY?editors=1010
When the tweet button is clicked then it redirect to the page to tweet in the twitter, with a preselected text to tweet.
The JavaScript code used there is given below just for a quick look:
//-------------quotes--------
(function(){
window.addEventListener("load", makeRequest);
function makeRequest(mk){
document.getElementsByClassName("buttonQ")[0].addEventListener("click", makeRequest);
function reqListener(rl) {
if(httpR.readyState === XMLHttpRequest.DONE) {
var quote;
if(httpR.status === 200) {
quote = JSON.parse(httpR.responseText);
document.getElementsByClassName("quote")[0].innerHTML = quote[0].body;
} else {
alert("There was a problem with the request!")
}
}
}
var httpR;
httpR = new XMLHttpRequest();
httpR.onreadystatechange = reqListener
httpR.open("GET", "https://quote-api.glitch.me/pull/1", true);
httpR.send();
}
//----------------------tweet-------------------
window.addEventListener("load", function() {
document.getElementsByClassName("buttonT")[0].addEventListener("click", tweetEvent);
})
function tweetEvent(twt) {
//twt.preventDefault();
document.getElementsByClassName("quote")[0].normalize();
var tweetBody = document.getElementsByClassName("quote")[0].childNodes[0].nodeValue;
var URLBase = document.getElementsByClassName("twitter-share-button")[0].getAttribute("href");
var URLExtended = URLBase + "?hashtags=quotes&text=" + encodeURIComponent(tweetBody);
document.getElementsByClassName("twitter-share-button")[0].setAttribute("href", URLExtended);
}
})();
Quirk:
when the tweet button is clicked for the first time after the page is loaded/refreshed then the preselected text in the redirected page to tweet is
Preselected_text(quote)_from_the_main_page #tweet
But after the first click, everytime the tweet button is click the preselected text in the redirected page to tweet is
Preselected_text(quote)_from_the_main_page?hashtags=quotes #quotes
Where i am doing wrong?
So I think the problem is that you are modifying the href of the anchor tag and inserting the modified href into the dom. What I would do instead is to get rid of the in the button and build the url like you are but instead of modifying something in the dom just call window.open(extendedUrl);
Something like this should get you started:
window.addEventListener("load", function() {
document.getElementsByClassName("buttonT")[0].addEventListener("click", tweetEvent);
})
function tweetEvent(twt) {
//twt.preventDefault();
document.getElementsByClassName("quote")[0].normalize();
var tweetBody = document.getElementsByClassName("quote")[0].childNodes[0].nodeValue;
var url = "https://twitter.com/intent/tweet?hashtags=quote&text="+encodeURIComponent(tweetBody);
return window.open(url);
}
})
As you can see I have simplified the url building and then passed the resulting url to window.open which will open the url in a new window/tab (depending on user preference in their browser... find more on that here).
I'm writing a basic Flask app. I have a page called /searchByCollege that consists of a bunch of buttons, each with a team name as their text, and .college as their class.
I've written some JS so that, when the user clicks on a button, it'll load /searchByCollege/collegeName, where collegeName is the text of the button they just clicked on. Here's what I have:
<script>
$('.college').on('click', function() {
var baseURL = $('#baseURL').text();
var finalURL = baseURL + "/" + this.text();
window.location.href = finalURL;
return false;
})
</script>
I didn't originally include return false; and nothing happened upon clicking a button. Then I added return false; and I got the same result. I've inspected the HTML and the base URL is correct (it's just /searchByCollege). I've looked at the requests as I click on the button and none are being made.
I've loaded jQuery above this through Google's CDN so that's not the issue.
Any other ideas?
Thanks for the help,
bclayman
this.text()
needs to be changed to
$(this).text()
You need to wait for the document to load by using $(document).ready:
<script>
$(document).ready(function(){
$('.college').on('click', function() {
var baseURL = $('#baseURL').text();
var finalURL = baseURL + "/" + this.text();
window.location.href = finalURL;
return false;
});
});
</script>
I could not figure out where you were getting the 'baseURL' Since you are using just JQuery you need to call .value and not .text()
<button class="college" value="CSU">CSU</button>
$('.college').on('click', function(){
var baseURL = "/searchByCollege";
var finalURL = baseURL + "/" + this.value;
return false;
});
you can try this
window.location.hostname = finalURL;
window.location.pathname = '';
The page URL has to change when the loadTest() function is called on a ahref click. But the page is getting refreshed only when alert is included in the code. If I remove the alert the same page is getting refreshed and not the new URL which I need.
<script type="text/javascript">
function loadText(prod_id)
{
curUrl = document.location.href.split("?");
document.location = curUrl[0]+'?prodId='+prod_id;
alert("Test");
}
</script>
use window.location instead of document.location
function loadText(prod_id)
{
curUrl = window.location.split("?");
window.location = curUrl[0]+'?prodId='+prod_id;
}
window.location = curUrl[0]+'?prodId='+prod_id;