Solved

Getting HTML5 history to work locally

Posted on 2014-02-28
1
1,083 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 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 39895800
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

840 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