16

When trying to redirect to an external page using $window.location.href, the page is just refreshing and not redirecting to the expected URL.

Brian
  • 14,610
  • 7
  • 35
  • 43
madh
  • 163
  • 1
  • 1
  • 5
  • 3
    $window.location = 'www.newlocation'; – Norbor Illig Mar 18 '15 at 18:43
  • eg. Inside the controller used like this e.g. $window.location = 'http://www.google.com' , but still it was not redirecting to new url. – madh Mar 18 '15 at 18:48
  • 1
    it works for me. Are you injecting $window into your controller? Doss it give you any error on the console? – Norbor Illig Mar 18 '15 at 18:49
  • Could just use plain javascript... `window.open('google.com');` https://developer.mozilla.org/en-US/docs/Web/API/Window/open – Tony Mar 18 '15 at 18:52
  • pdmApp.controller('projectionDetailController', [ '$log', 'projectionDetailService', '$filter', '$window', '$location', '$scope', function ($log, pdService, $filter,$window,$location, $scope) { $scope.backToPreviousPage = function () { $window.location = "http://www.google.com"; } } }); it is still not redirecting to external url e.g google.com – madh Mar 18 '15 at 18:57
  • Tony- I have used window.open('google.com') , this opens a new browser window. is it possible to open in existing window. – madh Mar 18 '15 at 20:34
  • @madh `window.open('google.com', '_blank')` I believe. – Tony Mar 18 '15 at 22:48
  • I saw this behavior happen when the URL was (literally) wrapped in quotes; even though I had "http://" in front of the URL. Removing the quotes fixed it. – Jason D Oct 08 '15 at 17:40

2 Answers2

30
<html ng-app="myApp">
<head>
</head>
<body ng-controller="myCtrl">
    <p> <a href="https://www.google.co.in/"><button>Click me to redirect from template</button></a></p>
    <p ng-click="myFunction()"> <button>Click me to redirect from controller</button></p>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script>
    var app = angular.module('myApp',[]);
    app.controller('myCtrl',function($window,$scope){
        $scope.myFunction = function(){
        $window.location.href = 'http://www.google.com'; //You should have http here.
        }
    });
</script>
</body>
</html>

This works for me.

Alaksandar Jesus Gene
  • 6,523
  • 12
  • 52
  • 83
  • Alaksandar ... I tried the same way with $window.location.href , the page refreshing instead of redirect to expected url. – madh Mar 18 '15 at 19:31
  • 1
    Are you sure that you got the http.Without http it will fall back your website only. – Alaksandar Jesus Gene Mar 18 '15 at 19:37
  • I am running under IISExpress(localhost) eg.http://localhost:port/.../../sample.aspx – madh Mar 18 '15 at 19:50
  • You are missing the http here $window.location.href = 'http://www.google.com'; with reference to your comments. Can you post your code of that $window.location – Alaksandar Jesus Gene Mar 18 '15 at 19:57
  • pdmApp.controller('projectionDetailController', [ '$log', 'projectionDetailService', '$filter', '$window', '$location', '$scope', function ($log, pdService, $filter,$window,$location, $scope) { $scope.backToPreviousPage = function () { $window.location = 'http://localhost:port/../sample2.aspx'; } } }); – madh Mar 18 '15 at 20:05
  • 1
    $window.location.href = "http://localhost:port/../sample2.aspx";; the http is missing out in comment. I will post as new answer – Alaksandar Jesus Gene Mar 18 '15 at 20:07
  • Hi,How will be the reverse navigation? From the remote url to localhost pages? – Arj 1411 Sep 16 '21 at 06:29
0
pdmApp.controller('projectionDetailController', [ '$log', 'projectionDetailService', '$filter', '$window', '$location', '$scope', function ($log, pdService, $filter,$window,$location, $scope) { 

$scope.backToPreviousPage = function () { 
  alert("function is working"); //Add this line to your code to confirm your function is called.
$window.location.href = "http://localhost:port/../sample2.aspx";

} 

} //What is this bracket for?


}]); //you are missing this square bracket.

Change To

pdmApp.controller('projectionDetailController', ['$log', 'projectionDetailService', '$filter', '$window', '$location', '$scope', function($log, pdService, $filter, $window, $location, $scope) {
  $scope.backToPreviousPage = function() {
    alert("function is working"); //Add this line to your code to confirm your function is called.
    $window.location.href = "http://localhost:port/../sample2.aspx";
  };
]);
Philipp Meissner
  • 5,273
  • 5
  • 34
  • 59
Alaksandar Jesus Gene
  • 6,523
  • 12
  • 52
  • 83