1

I have view page. It was done using Html.ActionLink. But now my boss said I need to click anywhere on the page should open another page. I have some code it will open new tab can you give some help

 <script type="text/javascript">
  var popup = function () {
   window.open('@Url.Action("UserLogin", "UserLogin")');
  }
 </script>
 <body onclick="popup()">
    <h1 class="dd">Click anywhere!</h1>
 </body>

And this will open top only. Can you help me out about this one? i need to click anywhere on the view page and open same page.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122

4 Answers4

1

You can use $(document).on("click", function(){}) function as shown:

$(document).ready(function() {
    $(document).on("click",function() {
        alert("It works.");
      window.location.href = "www.google.com";
    });
    })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p> Click anywhere! </p>
TheUnKnown
  • 681
  • 5
  • 29
0

try this

$( document.body ).click(function() {
alert('Hi I am bound to the body!');
});

Or the following

$('body').click(function(){
console.log('Hi I am bound to the body!');
});
Trilok Kumar
  • 571
  • 6
  • 12
0

Try specifying "_self", this should use the same tab.

<script type="text/javascript">
  var popup = function () {
   window.open('@Url.Action("UserLogin", "UserLogin")','_self');
  }
 </script>
mxmissile
  • 11,464
  • 3
  • 53
  • 79
0

I think others bring up good points and great ways to handle things but are misunderstanding your question.

In order for you to change your script to open in the same window rather a new window simply modify:

window.open('@Url.Action("UserLogin", "UserLogin")');

to:

window.location.href = '@Url.Action("UserLogin", "UserLogin")';

The one you are originally using is telling the browser to explicitly open it in a new window.

location on the other hand represent the URL (location) of the object that it is linked to (in this case since calling window.location, its object is the current window). When you change the location properties, the object it is linked to will reflect those changes thus your window's location will change (giving you a current page redirect.)

Travis Acton
  • 4,292
  • 2
  • 18
  • 30
  • working thanks,and also i want to click page any where now it is working only top of side can you tell me how to do that – Varun chehan Jun 05 '19 at 22:38