Solved

Getting HTML5 history to work locally

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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
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…

728 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