Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Getting HTML5 history to work locally

Posted on 2014-02-28
1
Medium Priority
?
1,262 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 84

Accepted Solution

by:
Dave Baldwin earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Suggested Courses

810 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