Solved

Javascript syntax

Posted on 2012-04-09
15
419 Views
Last Modified: 2012-08-14
I have the following javascript, but something in the syntax is wrong, perhaps someone who knows syntax can help. I get an error when running the ASP/Javascrip page on this lines:

<%
function isIPad() {
alert( Request.ServerVariables("HTTP_USER_AGENT") );
    var isIpad = false;
alert( Request.ServerVariables("HTTP_USER_AGENT") );
    if ( userAgent.indexOf("iPad") > 0 )
        isIPad = true;

    return isIpad;

}
%>


The code detects if the user is using an ipad or not.
0
Comment
Question by:amucinobluedot
  • 8
  • 2
  • 2
  • +3
15 Comments
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37823268
0
 

Author Comment

by:amucinobluedot
ID: 37823298
Thanks but I really don't know much of Javascript, what i do is display a region in the ASP based on user using ipad or not:

<%if ( isIPad() ) { %>
    Code for text field here
<% } else { %>
    Code for HTML editor here
<% } %>

... that being said what I need is to return 'isPad' value and not redirecting anywhere, so the javascript is quite different.
0
 
LVL 20

Assisted Solution

by:Proculopsis
Proculopsis earned 84 total points
ID: 37823340


// I think you need something like this:

function isIPad() {
alert( "<%= Request.ServerVariables("HTTP_USER_AGENT") %>" );
    var isIpad = false;
    var userAgent =  "<%= Request.ServerVariables("HTTP_USER_AGENT") %>";
    if ( userAgent.indexOf("iPad") > 0 )
        isIPad = true;

    return isIpad;

}
0
 

Author Comment

by:amucinobluedot
ID: 37823372
Got this error:

Microsoft JScript compilation error '800a03ee'

Expected ')'

/bluedot/Intranet/Cases/Commentspop/AddComments.asp, line 289

alert( "<%= Request.ServerVariables("HTTP_USER_AGENT")
0
 
LVL 9

Assisted Solution

by:Chirag1211
Chirag1211 earned 83 total points
ID: 37823396
I think the below code should work for you

<script type='text/javascript'>
      function isIPad(){
            var isIPad = false;
            var userAgent = '<%=Request.ServerVariables("HTTP_USER_AGENT")%>';
            alert(userAgent);
            if(userAgent.indexOf("iPad") > 0) isIPad = true;
            return isIPad;
      }
</script>
0
 

Author Comment

by:amucinobluedot
ID: 37823448
Seems like that worked, the second part of the code gave me an error though ... here it is:

 <%if ( isIPad() ) { %>

        <label for="Comments"></label>
        <textarea name="Comments" cols="50" rows="8" id="Comments"><% if (!Lettrmrg.EOF || !Lettrmrg.BOF) { %> <%=(Lettrmrg.Fields.Item("LtrBody").Value)%>  <% } // end !Lettrmrg.EOF || !Lettrmrg.BOF %>  
        </textarea>
<% } else { %>
     <textarea id="Comments" name="Comments" class="dmxEditor" style="width:600px;height:300px"><% if (!Lettrmrg.EOF || !Lettrmrg.BOF) { %> <%=(Lettrmrg.Fields.Item("LtrBody").Value)%>  <% } // end !Lettrmrg.EOF || !Lettrmrg.BOF %>  
</textarea>
<script type="text/javascript">
  // <![CDATA[
 jQuery(document).ready(
   function()
     {
       jQuery("#Comments").dmxEditor(
         {}
       );
     }
 );
  // ]]>
</script>
<% } %>

-- the error is in the first like of the code --

Microsoft JScript runtime error '800a138f'

Object expected

/bluedot/Intranet/Cases/Commentspop/AddComments.asp, line 506

-------

I didnt use to get an error on this part of the code ... maybe a naming is not the same as the first part of the code ?
0
 

Author Comment

by:amucinobluedot
ID: 37823536
To keep things simple this is the bottom code that displays the region based on the value captured.

 <%if ( isIPad() ) { %>
 code for text region here
<% } else { %>
 code for HTML editor here
<% } %>

<%if ( isIPad() ) { %>

I get the error above on line # 1.  Seems like the value is not being captured ?
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:amucinobluedot
ID: 37823554
As a matter of fact the logic that we should have in the javascript is the following:

Function isIPad()
    If InStr(Request.ServerVariables("HTTP_USER_AGENT"), "iPad") > 0 Then
        isIPad = True
    Else
        isIPad = False
    End If
End Function

The above is VBscript though ... same logic but in javascript, i dont think the current logic is the same for:

<script type='text/javascript'>
      function isIPad(){
            var isIPad = false;
            var userAgent = '<%=Request.ServerVariables("HTTP_USER_AGENT")%>';
            alert(userAgent);
            if(userAgent.indexOf("iPad") > 0) isIPad = true;
            return isIPad;
      }
</script>
0
 

Author Comment

by:amucinobluedot
ID: 37823561
I also tried using vbscript code in the javascript page:

<script type="text/vbscript">

Function isIPad()
    If InStr(Request.ServerVariables("HTTP_USER_AGENT"), "iPad") > 0 Then
        isIPad = True
    Else
        isIPad = False
    End If
End Function

 </script>

then below ...

<%if ( isIPad() ) { %>

        <label for="Comments"></label>
        <textarea name="Comments" cols="50" rows="8" id="Comments"><% if (!Lettrmrg.EOF || !Lettrmrg.BOF) { %> <%=(Lettrmrg.Fields.Item("LtrBody").Value)%>  <% } // end !Lettrmrg.EOF || !Lettrmrg.BOF %>  
        </textarea>
<% } else { %>
     <textarea id="Comments" name="Comments" class="dmxEditor" style="width:600px;height:300px"><% if (!Lettrmrg.EOF || !Lettrmrg.BOF) { %> <%=(Lettrmrg.Fields.Item("LtrBody").Value)%>  <% } // end !Lettrmrg.EOF || !Lettrmrg.BOF %>  
</textarea>
<script type="text/javascript">
  // <![CDATA[
 jQuery(document).ready(
   function()
     {
       jQuery("#Comments").dmxEditor(
         {}
       );
     }
 );
  // ]]>
</script>
<% } %>

Same error:

Microsoft JScript runtime error '800a138f'

Object expected

/bluedot/Intranet/Cases/Commentspop/AddComments.asp, line 510
0
 

Author Comment

by:amucinobluedot
ID: 37823570
If I get rid of the bottom code the page works fine with the javascript code at the top. So right now the bottom code is the issue. I will open a different ticket for this issue thanks for the javascript code help.
0
 
LVL 9

Assisted Solution

by:Sar1973
Sar1973 earned 166 total points
ID: 37827050
You can include a Javascript function in your web page HTML code
<script language="Javascript">
function isIPad() {
    alert(Request.ServerVariables("HTTP_USER_AGENT") );
    var isIpad = false;
    alert( Request.ServerVariables("HTTP_USER_AGENT") );
    if ( userAgent.indexOf("iPad") >= 0 ) {
        isIPad = true;
         return isIpad;
    } else {
    }
}
</script>
</body>
</html>

or in a separate .js file, calling it like this:
<script language="JavaScript" src="amucino.js">
<!--
-->
</script>

Please notice that your original function I have reported is slightly different, since on line 5 it's better to declare ">=0" in the indexOf function, because the string you're looking may be exactly at position 0. Moreover you have a couple of { missing at the end, such as the if declaration end wiht else statement.
Finally it depends on you and on the function itself (I haven't really examined its steps) where to put the different commands (inside or outside the if declaration); I have only noticed a double alert which should be elsewhere.
0
 

Author Comment

by:amucinobluedot
ID: 37827862
Ok, so I used the code above it didnt give me an y errors on any of those lines, but below in the page i get this error (Part where i display content based on the above code)

This is the base code i use to display content:

<%if ( isIPad() ) { %>
 code for text region here
<% } else { %>
 code for HTML editor here
<% } %>

But it gives me this error:

Microsoft JScript runtime error '800a138f'

Object expected

/bluedot/Intranet/Cases/Commentspop/AddComments.asp, line 515


Anything i am missing ?
0
 
LVL 9

Assisted Solution

by:Sar1973
Sar1973 earned 166 total points
ID: 37827893
If it's so precise, you should first check the code itself at the specified line: probably you missed/misnamed a declaration. Are you now in case isIpad or not?
0
 
LVL 18

Assisted Solution

by:nap0leon
nap0leon earned 167 total points
ID: 37833005
Minor capitalization issue in Sar1973's function
When it tries to set "isIpad = true" you actually have "isIPad=true"

isIPad is the name of your function
isIpad is the value you are setting and returning

Also, Request.ServerVariables("HTTP_USER_AGENT")  is not valid javascript, so you need to wrap it in <% %> and put quotes around it.

Also, the variable 'userAgent' is never set

Here is the capitalization corrected function:
<div id="content"></div>
<script language="Javascript">
function isIPad() {
    var userAgent = '<%=Request.ServerVariables("HTTP_USER_AGENT")%>'
    var isIpad = false;
    if ( userAgent.indexOf("iPad") >= 0 ) {
        isIpad = true;
    } else {
    }
    return isIpad;
}

var iPad = isIPad()
if (iPad){
	document.getElementById('content').innerHTML = "This is an iPad"
} else {
	document.getElementById('content').innerHTML = "This is NOT an iPad"
}
</script>

Open in new window

0
 
LVL 18

Accepted Solution

by:
nap0leon earned 167 total points
ID: 37833038
To make you isIPad function a bit shorter (and also since I am not sure of the capitalization of "iPad" in the HTTP_USER_AGENT, try this instead:

function isIPad() {
	('<%=lcase(Request.ServerVariables("HTTP_USER_AGENT"))%>'.indexOf("ipad") >= 0) ? isIpad = true : isIpad = false
	return isIpad
}

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now