angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

ngHref adds two history records in legacy browsers

Open jirikuchta opened this issue 12 years ago • 9 comments

Clicking on a link with ngHref directive in a legacy browser (IE8-9) will adds two records into browser history. There are need two clicks on back button to get back to previous page then.

Here is a sample application that will reproduce this behavior (needs to be run IE8-9): (sorry I didn't manage to create a working plunkr example)

"use strict";

var App = angular.module("App", ["ngRoute"]);

App.config(["$routeProvider", "$locationProvider", function ($routeProvider, $locationProvider) {

    $routeProvider.when("/", {
        controller: function ($scope) {
            $scope.url = "/second";
        },
        template: "<a ng-href=\"{{url}}\">Click me!</a>"
    });

    $routeProvider.when("/second", {
        template: "<h1>Now click on browser back button.</h1>"
    });

    $routeProvider.otherwise({redirectTo: "/"});

    $locationProvider.html5Mode(true);
    $locationProvider.hashPrefix("!");

}]);

I'm running AngularJS v1.2.15.

Can you please confirm this bahaviour?

jirikuchta avatar Sep 09 '14 17:09 jirikuchta

Speaking of this, @tbosch should we backport the location fixes you wrote into 1.2.x? They still have the broken url parser on click. I would be willing to bet this issue is related to that.

caitp avatar Sep 09 '14 17:09 caitp

Yes, we will. Just waiting a little longer for feeback...

tbosch avatar Sep 09 '14 23:09 tbosch

can we verify if this still happens with 1.3.x? if it does let's look into it, otherwise let's close this as fixed.

IgorMinar avatar Sep 30 '14 20:09 IgorMinar

Will look into this tomorrow...

tbosch avatar Oct 02 '14 01:10 tbosch

Cannot reproduce on IE9 nor IE8 and Angular 1.2.15 nor latest in all of the following cases:

  • without tag:
    • loading from "/" or loading from "/index.html"
  • with tag

I used the following code (can't use plunker as the code listed above is meant to be run on /):

<!DOCTYPE html>
<html ng-app="App">
<head>
  <!--
  <script>document.write('<base href="'+location.href+'">');</script>
-->
  <script src="https://code.angularjs.org/1.2.15/angular.js"></script>
  <script src="https://code.angularjs.org/1.2.15/angular-route.js"></script>

  <script>
    var App = angular.module("App", ["ngRoute"]);

    App.config(["$routeProvider", "$locationProvider", function ($routeProvider, $locationProvider) {

      $routeProvider.when("/", {
          controller: function ($scope) {
              $scope.url = "/second";
          },
          template: "<a ng-href=\"{{url}}\">Click me!</a>"
      });

      $routeProvider.when("/second", {
          template: "<h1>Now click on browser back button.</h1>"
      });

      $routeProvider.otherwise({redirectTo: "/"});

      $locationProvider.html5Mode(true);
      $locationProvider.hashPrefix("!");

  }]);
    App.run(function($location, $rootScope) {
      $rootScope.$on('$locationChangeSuccess', function() {
        console.log('$locationChangeSuccess');
      });

    });
  </script>
</head>
<body>
  <div ng-view></div>
</body>
</html>

tbosch avatar Oct 03 '14 17:10 tbosch

Could you provide another reproduction case?

tbosch avatar Oct 03 '14 17:10 tbosch

this is known to happen when iframes/objects are involved (which is obviously not showing up in the pasted code) --- iframes really screw up the history API

caitp avatar Oct 03 '14 17:10 caitp

Hm, that is weird. I have tried everything to reproduce the bug again, but with no luck. It just works now.

I guess we can close this issues. If I run into this behavior again I will try to provide more information.

Thanks for the effort!

jirikuchta avatar Oct 04 '14 08:10 jirikuchta

Closing as suggested...

tbosch avatar Oct 08 '14 18:10 tbosch