1

I have four php pages:

  1. header.php
  2. demo1.php
  3. demo2.php
  4. demo3.php

I am including header.php in every page i.e demo1.php, demo2.php and demo3.php. I included common JavaScript required for demo1.php, demo2.php and demo3.php in header.php.

header.php code:

<script type="text/javascript" src="jscript/jquery.js"></script>
<script type="text/javascript" language="javascript">
$(function(){
     $("#btn1").click(function(){
       console.log("btn1 clicked");
      });
     $("#btn2").click(function(){
      console.log("btn2 clicked");
     });

});
</script>

btn1 and btn2 elements are not declared in header.php, but they are declared in included pages like demo1.php, demo2.php and demo3.php.

demo1.php code:

 <?php include "header.php";?>
 <input type="button" id="btn1" value="check"/>
 <input type="button" id="btn2" value="check"/>

This demo1.php page works in all browsers except in IE.

In IE it showing error Object expected at $("btn1").click(function(){});.

Can any one help me achieving this problem?

pimvdb
  • 151,816
  • 78
  • 307
  • 352
Ramesh Paul
  • 840
  • 4
  • 15
  • 31
  • 1
    Did you have the console open? If the console isn't open, IE will fail on console.log – Kevin B Oct 19 '12 at 15:23
  • As @KevinB said there is no console object available unless you open the developer tools.. So try using alert instead of console.log or instantiate the console object on your page – Sushanth -- Oct 19 '12 at 15:26
  • for console i am using this code `if(!window.console){console.log("btn1 clicked");}` – Ramesh Paul Oct 19 '12 at 15:26
  • I guess the console is open, otherwise you can't notice the error. – pimvdb Oct 19 '12 at 15:26
  • Yeah that's what i expected, i didn't think console was the problem because it doesn't match the error that is happening. does removing `type="text/javascript" language="javascript"` make any difference? – Kevin B Oct 19 '12 at 15:28
  • ya i tried with removing `type="text/javascript" laguage="javascript"` but it is showing same error – Ramesh Paul Oct 19 '12 at 15:30
  • Is jQuery loaded correctly? Can you provide a http://jsfiddle.net demo? – pimvdb Oct 19 '12 at 15:31
  • jquery loaded correctly except that code remaining jquery code is working fine – Ramesh Paul Oct 19 '12 at 15:39
  • This seems to work: http://jsfiddle.net/Jb9fY/ @pimvdb I've added the jQuery on the left, otherwise I think it's the same as your example. – Jared Farrish Oct 19 '12 at 15:39
  • I wrote javascript in header.php page and this header.php page is included in demo1.php page, demo1.php contains actual DOM elements i.e `` – Ramesh Paul Oct 19 '12 at 15:47

1 Answers1

0

You may have an implicit semicolon or append at this line:

$("#btn1").click(function(){

It could also be an issue caused by the base element.

Community
  • 1
  • 1
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265