window location with the same page(div id) - javascript

How to make window.location go to the same page but with another different div tag(or through div id like this: href="#id").
if((mins == 0) && (secs == 0)) {
window.alert("Time is up.Your score is "+score); // change timeout message as required
window.location = "index.html" // redirects to specified page once timer ends and ok button is pressed
} else {
cd = setTimeout("redo()",1000);
}
any help would be appreciated.
thanks in advance.

You can use .scrollIntoView() on the targeted element.
document.getElementById("the_div_id").scrollIntoView(true);
MDN scrollIntoView()

Just add the hash as a string:
window.location + '#id';

Related

How to hide an element only if it matches specific hash?

I'm trying to hide a form only if it's on the root site, or on the specific hash https://www.example.com/#uno. My code below is causing the form is not show on all the subpages (/#dos, /#tres, etc). Can someone help me understand what is happening?
$(function(){
if (location.hash == "" || location.hash == "#uno"){
$("#form").hide();
} else {
$("#form").show();
};
}());
This is because you hide the form once, but you don't make it reappear until you reload the page. (You only check the hash once, when loading the entire page, not when the hash changes.
function showHideForm() {
if (location.hash == "" || location.hash == "#uno"){
$("#form").hide();
} else {
$("#form").show();
};
}
window.onhashchange = showHideForm;

How to decrease number of links in this Script by assing a single Variable

Any Javascript Expert There how help me in this script ?
Actually i am a template designer, i create free and premium templates, my free templates include non-removal footer links, which means user will have to keep the footer link for my template.
Come to main point.
here is my script:
<script>
window.onload = function() {
var e = document.getElementById("credits");
setInterval(function() { // check every 2 seconds
if (e == null) { // if div is removed then redirect
window.location.href = "http://www.example.com/"
}
// if div is hidden with css visibility then redirect
if ($(e).css('visibility') == 'hidden') {
document.location.href = "http://www.example.com";
}
// if div is display none then redirect
if ($(e).css('display') == 'none') {
document.location.href = "http://www.example.com";
}
// if div is hidden with css collapse then redirect
if ($(e).css('visibility') == 'collapse') {
document.location.href = "http://www.example.com";
}
}, 2000) // close time function
e.setAttribute("href", "http://www.example.com/");
e.setAttribute("ref", "dofollow");
e.setAttribute("title", "Blogger Templates");
e.innerHTML = "Example"
}
</script>
<a id="credits"></a>
Now, if you see the above script, you will observe that it uses 4 http://www.example.com links except e.setAttribute("href", "http://www.example.com/");. whilte i want to do some tricks with this JS so that i uses only one time the main link http://www.example.com by assign variable and use that instead of full link.
Like this:
<script>
window.onload = function() {
var e = document.getElementById("credits");
setInterval(function() { // check every 2 seconds
if (e == null) { // if div is removed then redirect
window.location.href = "e"
}
// if div is hidden with css visibility then redirect
if ($(e).css('visibility') == 'hidden') {
document.location.href = "e";
}
// if div is display none then redirect
if ($(e).css('display') == 'none') {
document.location.href = "e";
}
// if div is hidden with css collapse then redirect
if ($(e).css('visibility') == 'collapse') {
document.location.href = "e";
}
}, 2000) // close time function
e.setAttribute("href", "http://www.example.com/");
e.setAttribute("ref", "dofollow");
e.setAttribute("title", "Blogger Templates");
e.innerHTML = "Example"
}
</script>
As you see i added "e" variable instead of full link but it does not work, it is redirecting to example.com/e which is not valid. i gave you the idea. so make it possible
So, how to do this ? i already assign "e" variable to my main link.
Please share the full coding with me thanks.
Ok. First off, "e" is NOT a variable, it is a string literal. Which means you are literally assigning "e" to document.location.href. Additionally, you have previously created a variable named "e", and assigned it the value of a DOM element with the ID "credits."
I'm not 100% clear on what you're trying to do, but the following might help:
<script>
window.onload = function() {
var e = document.getElementById("credits");
var redirect = 'http://www.example.com';
// check every 2 seconds
setInterval(function() {
// if div is removed then redirect
if (e == null) {
window.location.href = redirect
}
// if div is hidden with css visibility then redirect
if ($(e).css('visibility') == 'hidden') {
document.location.href = redirect;
}
// if div is display none then redirect
if ($(e).css('display') == 'none') {
document.location.href = redirect;
}
// if div is hidden with css collapse then redirect
if ($(e).css('visibility') == 'collapse') {
document.location.href = redirect;
}
}, 2000) // close setInterval
e.setAttribute("href", redirect);
e.setAttribute("ref", "dofollow");
e.setAttribute("title", "Blogger Templates");
e.innerHTML = "Example"
}
</script>
The "e" you've added isn't a variable, it's a string. Setting it as the href attribute will just set href="e", which will link you to /e on the current domain.
You need to use either:
var siteName = "http://www.example.com";
// Wherever you need it:
document.location.href = siteName;
Or just use the OR operator, ||, to combine your if statements. You can repeat this for all cases.
if (e == null ||
$(e).css('visibility') == 'hidden') {
document.location.href = "http://www.example.com";
}
Other than that, what you're trying to do is really a bad practice. I'd advice against it.

Javascript that gets current url and changes page appearance

I need JS code that does the following
1.) Get's the current url of the page.
2.) If url = a certain url
Then = #header is set to display:none;
3.) If else = no change
Came up with a few ideas with no avail, struggling with this; appreciative of any and all help.
try this
document.getElementById("header").style.display =
window.location.url == 'certain url' ? "none" : "block";
OR use IF condition
if(window.location.url == 'certain url'){
document.getElementById("header").style.display = 'none';
}else{
document.getElementById("header").style.display = 'block';
}
if (document.URL == 'URL'){
document.getElementById("header").style.display="none";
}
You can try this.
if(window.location.href == 'someurl')
document.getElementById('header').style.display='none'
if (window.location.url == 'link here'){
document.getElementById('header').style.display='none'
else{
document.getElementById('header').style.display='block'
}
To get the URL of the current page use document.URL in your javascript.
You can hide the header using $('#header').hide() or document.getElementById('header').style.display = "none" if the url comparison evaluates to true.

how to use javascript for redirecting parent page with conditional alerts?

I'm currently using this code:
var domains = ["domain.net", "domain.com", "domain.info"];
if (domains.indexOf(document.location.hostname) == -1 && document.cookie.indexOf('redirected=1') == -1)
{
window.top.location="http://domain.net";
document.cookie = "redirected=1";
alert("Redirecting to our main website");
}
else if (domains.indexOf(document.location.hostname) == 1)
{
document.cookie = "redirected=1";
}
alert("Thanks for joining our main website");
if i move the second alert over "}" it will not give any alert, and when i leave it as it is i will get sequential alerts starting with 1st alert and then second alert, then when page redirects i will get second alert again.
is there a way to fix this ?
Thanks in advance

JQuery Keyup event doesn't work

So, I need use this event so I can navigate trought blog posts. I use the 'J' key to go to the previous post and the 'K' key to go to the next post.
My problem is that the event works on the first try but then doesn't work anymore.
When i restart the browser it works if i press J or K and it redirects me to the previous/next post. But then if I press again it does nothing.
Sorry if I can't explain it exactly enought and thanks for helping.
$(document).keyup(function (event) {
if (event.keyCode == 74) {
var left_link = $('#nav-left a').attr('href');
alert(left_link);
if(typeof left_link !== 'undefined' && left_link !== false)
window.location = left_link;
}
else if (event.keyCode == 75) {
var right_link = $('#nav-right a').attr('href');
alert(right_link);
if(typeof right_link !== 'undefined' && right_link !== false)
window.location = right_link;
}
});
Even if I don't do the redirect and only those alerts it doesn't work.
After you've changed the location, a new document is loaded. A document without a listener for keyup events.
There was a problem with the script loading, I applied a delay of 1 second and now it works just fine.
Thanks anyway.

Categories

Resources