Solved

Getting HTML5 history to work locally

Posted on 2014-02-28
1
1,107 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

730 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