Solved

Getting HTML5 history to work locally

Posted on 2014-02-28
1
1,009 Views
Last Modified: 2014-02-28
I have the following HTML5 code I got from this page: http://html5.gingerhost.com/seattle

I downloaded the code and tried to get it working locally but haven't had any success. Here is the error:

SecurityError: The operation is insecure.
file:///C:/Users/miles.willis/Desktop/html5location/HTML5%20History%20API%20_%20cyberstream.us.htm
Line 86

      history.pushState(page_to_load, page_to_load, page_to_load )

Is there anyway to get this to work locally? I'm using FF browser.
<br/>

			  <ul id="menu" class="clearfix"> 
                <li class="current"><a href="/">Home</a></li>
                <li><a href="seattle">Seattle</a></li>
                <li><a href="new-york">New York</a></li>
                <li><a href="london">London</a></li>
            </ul>
 
<br/>

<script>
	// THIS IS WHERE THE MAGIC HAPPENS
		$(function() {
			$('nav a').click(function(e) {
				$("#loading").show();
				href = $(this).attr("href");
				
				loadContent(href);
				
				// HISTORY.PUSHSTATE
				history.pushState('', 'New URL: '+href, href);
				e.preventDefault();
				
				
			});
			
			// THIS EVENT MAKES SURE THAT THE BACK/FORWARD BUTTONS WORK AS WELL
			window.onpopstate = function(event) {
				$("#loading").show();
				console.log("pathname: "+location.pathname);
				
			};

		});
	
	</script>

Open in new window

0
Comment
Question by:894359
1 Comment
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
Comment Utility
From https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history :
The new URL can be any URL in the same origin as the current URL.

You are not working thru a web browser since the error URL starts with file:///C:/Users/miles.willis/Desktop/ .  There are many things in modern browsers that will no work thru the 'file://' protocol because there is no 'domain' associated with it.  Cookies, local storage, and apparently 'pushstate' are some that have security restrictions applied to them.  Some times they won't even work thru 'localhost' because it is not a unique domain.

If you put your code on a web server, it might start working.  It at the very least will not have that particular error message.

A complicated document about browser security and restrictions: http://code.google.com/p/browsersec/wiki/Part2
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now