Link to home
Start Free TrialLog in
Avatar of jazzIIIlove
jazzIIIloveFlag for Sweden

asked on

inject aspx code to javascript or how to reach postback in javascript?

Hi;

I am trying to reach postback in front in javascript and inject the given html

<asp:PlaceHolder ID="myPlaceholder" runat="server">

<script type="text/javascript">
    
    if(<%= Page.IsPostBack()%>) /// Fails here
    {
        document.write("<!DOCTYPE html>");
        document.write("<html>");
        document.write("<head>")
        document.write("<meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'>");
        alert("postback");
    }

Open in new window


or how can i reach postback in front?

I also add the Client register code but failed to operate it..

Regards.
Avatar of guru_sami
guru_sami
Flag of United States of America image

Try this:
var postback ='<%= Page.IsPostBack%>';
if(postback=='True') /// Fails here
{
   alert("postback");
 }

Open in new window

Avatar of Miguel Oz
For a pure javacript solution (No server variables required)
<script type="text/javascript">
 function isPostBack()
 {
  return document.referrer.indexOf(document.location.href) > -1;
 }

 if (isPostBack()){
  //Your code goes here  
  alert("postback");
}

Open in new window

Avatar of jazzIIIlove

ASKER

Hi guru_sami;

The thing is that the code works but once it is postback, it's already too late that i cannot set DOCTYPE. Is there a way for this?

<script type="text/javascript">

    var postback = '<%= Page.IsPostBack%>';
    alert(postback);
    if (postback == 'True')
    {
        document.write("<!DOCTYPE html>");
        document.write("<html>");
        document.write("<head>")
        document.write("<meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'>");       
    }
   

Open in new window

mas_oz2003:
Same for you too.

Guys, the thing is i have an if check in codebehind, if it's not postback, i have to set to the doctype to another type.

That's the reason of this hassle.

Regards.
ASKER CERTIFIED SOLUTION
Avatar of guru_sami
guru_sami
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial