I know there is plenty on the subject but not what exactly i want...
I've tried window.location.replace("http://gmail.com")but it replaces the content only in a specific <div> in the page instead of the whole page..
In the w3schools tutorials of location.replace for example
http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_loc_replace
It allows you to load the new URL in the box on the right of the page.
In my case, i want a javascript that will replace the whole page, and not only in the small box on the right.
I hope i was clear enough.
It sounds like you've got code in an <iframe>. If so, you can reload the whole browser window/tab with
window.top.location.replace("http://gmail.com");
Related
i will try to explain very simple:
I'm calling a div trough a href tag like this:
Injectie
<div id="injectie">
<p>some text</p>
</div>
It's working well but I need to achieve this thing: let's say my link of the page is www.example.com . i would want that when i click the link to call the div the url of my page will change in something like this:
www.example.com/#injectie but without leaving the page. The way I made it it only calls the div but without changing the url.
Is there a way to make this happen?
What you could do is put the page url into the link, instead of only the anchor's ID.
Injectie
<div id="injectie">
<p>some text</p>
</div>
Since you don't want your page to reload, this question might be helpful:
Modify the URL without reloading the page
Yes instead you need to use scroll to function available in Jquery which will not change the URL but scrolls to the element you want.
.scrollTo( target, options, [, complete] )
Ex:$('body').scrollTo('#injectie');
to change the url without reload use this
function processAjaxData(response, urlPath){
document.getElementById("content").innerHTML = response.html;
document.title = response.pageTitle;
window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"", "www.example.com/injectie");
}
It's not possible to change the whole url to another website without loading this website directly for serious security reasons.
Spoofing the actual website you are on would be way too easy and fisher would cerebrate a huge blowout.
You only can change your url behind the # without triggering a website reload on the client side.
And if you find a way to do the impossible nonetheless i wouldn't be surprised if you get a bounty from the affected browser company. ;)
Edit:
as Tirupathi Raju pointed out its possible to change the html5 browser history so you can change everything after the domain/ip eg. http://example.com/<here you could fiddle around>
I am not sure if there is a name for what I want to do, but I would like to use javascript to alter every url on a webpage on my website. So if someone posts a url such as:
visit http://www.blah.com
when viewing this page, I want javascript to change it to:
visit http://www.mysite.com/count.php?out=http://www.blah.com
I know about the two urls in one I will handle that part, I just used this as an example of placing the same text in front of every url in the page. Kinda like how thewaybackmachine does it. Thanks.
Using jQuery:
$("a").attr('href', "http://test.com?blah=" + $(this).attr('href'));
This code searches for all a tags on the page, and replaces the contents of the href attribute with http://test.com?blah= followed by the original contents of the href attribute.
To try it out, You can copy it, hit 'f12', paste it into the console, hit enter, and see it's effects on this page right here! (also breaking all your links...)
Here's a way, without the need for JQuery. (loading and parsing a pretty big library in order to just do one simple thing may be a bit much..)
[].slice.call(document.querySelectorAll('a')).map(function(a){ a.href = 'http://somesite.com/count.php?' + a.href })
Ok, this is my problem... I have two pages. On the first page I have a header (h2) element on which I want to apply a border style. On the second page, there is only one link. When clicking on the link from the second page, to navigate to the first page, it should create a border on the first page around the h2 tag.
Any thoughts on how to do this? Is it possible to do it with regular JavaScript or jQuery?
Thanks!
No, JavaScript is client-side and for this you would require the server to remember if the link was clicked, possibly by recording this in a database or session variable.
That's of course if you're using the tradition model of a website, rather than loading pages already using pure JS.
It would be a pretty stupid way of doing it, but it is possible to do it client side. I would seriously recommend to do it server-side though.
On page 2, link back to page 1 with a hash:
Go back to page one and add a border
And on page 1, check if there's a hash:
if (window.location.hash === '#border') {
$('h2').addClass('withBorder');
}
I think if you are looking this kind of scenario you can achieve it with passing some hash to the url:
i passed a hash named 'second' in the second page url and put this script on second page
$(function(){
var url=window.location;
var hash = url.hash.substr(1);
if(hash == "second"){
$('h2').css('border','solid 1px red');
}
});
Checkout this if helps.
Well there is a way you could do this with JavaScript, although it's tricky and server side is a LOT easier. You would need to use some JavaScript to load different pages without refreshing the entire DOM. I do this with something called pjax. The way it works is to have each page act as a container to load all subsequent pages via ajax. By not doing a full page reload, any style changes you make on one page get carried over to other pages (this dose not survive an actual browser refresh).
So I have this js code for an image gallery:
(this.settings.update_window_hash) {
var thumb_link = this.images[this.current_index].thumb_link;
if (thumb_link.attr("id")) {
window.location.hash = "#image-"+ thumb_link.attr("id"); //#url
} else {
window.location.hash = "#image-"+ this.current_index;
};
};
So as you've probably assumed this appends $image-(int) to the url. So if I have a
gallery with multiple images if the thir image is selected the url will look like this:
mysite.com/gallery.html#image-3
All good. But I dont really like this to be appended to the end of the url. So is there
any problem if I remove this part of the script entirely? So regardless the number of
image currently selected the url will look like this:
mysite.com/gallery.html
I've tested it and it works okay. But I'm not very experienced with javascript and I want
to make sure I'm not making a mistake. So
IS IT OKAY IF I REMOVE THIS SCRIPT ENTIRELY? WILL IT CAUSE ANY PROBLEMS?
HUGE THANKS.
Hashes at the end of the URL are optional and not required so YES, you can remove that script if you want (I'm not sure what problem you're trying to solve by removing it). In general, you get more useful answers if you tell us what problem you're trying to solve rather than what solution you're trying to use.
Hashes are used when you want the URL of the page to direct the viewer to some subcontent on that page. If you remove them, your page will still work just fine, but the URL of the page will not reflect which image is displaying. So, if the viewer saves that URL and comes back to it or links to it or anything that keeps a reference to the URL, it will go to the generic version of the page, not the onethat shows a specific image. Whether that is OK is totally up to you and how your page works.
Just use:
location.replace(location.href + "#myhash");
The location.replace method overwrites the current step in browser history. For an example of this in action see http://prettydiff.com/slideshow/
The stuff after the octothorpe normally represents a "name" or "id" from the web page. You can have an anchor tag (<a name='thevalue'>) and the browser will interpret the text after the octothorpe (http://example.com#thevalue) by scrolling to the associated section on the page.
Unless the page has special JavaScript to behave differently. In your case, it depends upon the full functionality of the web page you're writing. If you have smoke tests/unit test/use case tests/other QE tests, you should execute those to ensure that your changes don't break anything.
See http://www.w3schools.com/html/html_links.asp for more description of the standard usage.
Have a look at http://keithloutit.com/#news
I have a similar site, with all the content on the same page.
I would like to know how to fetch the correct part of the url. In this case it would be "news". I suppose its some regexp used on document.location.href? Hope you get the idea, otherwise ill try to elaborate. Thanks
The javascript on the page has to get the hash from the URL:
window.location.hash
and then activate the currect tab.
Note: if you're merely linking to a part of your page that is not part of a tabular system, you don't need javascript. The page will automatically scroll down to the element with that ID.