jquery colliding with my javascript

Posted on 2011-03-10
Last Modified: 2012-05-11
I have some jquery inside an user control on my page that works fine UNTIL I add a small javascript block inside my <head> section. It doesn't matter what I put inside that js block, even a simple alert, nothing works - because of the jquery. If I comment out all the jquery stuff and remove the reference to jquery, my custom js code works fine.

In my custom js block I've tried putting my stuff inside:

$(document).ready(function () {

Open in new window

...but that $(document).ready block already appears further down, inside the control. And I'm thinking you can only call $(document).ready once on a page. Is that right?

What I'm trying to accomplish is this:

window.onresize =
        function () {

        function resizeDiv() {
            if (document.body) {
                var divMainHandle = document.getElementById("divMain");
                if (divMainHandle) {
           = (document.body.clientHeight - 220) + "px";

Open in new window

So resizeDiv() gets called when the browser is resized, AND I want it to be initially called on page load. But all of these attempts break, and throw js errors, when there is jquery on the page.

How can I get my js code to play nicely with jquery and to work correctly?
Question by:SweatCoder
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 35098576
change your $(document).ready

LVL 11

Author Comment

ID: 35098633
dx, that didn't work. Right now the only thing in my js block inside <head> is:

jQuery(document).ready(function () {
            //$(window).resize(function () {
                alert("got here!");

Open in new window

So I should get the alert, right? Sadly I don't.
LVL 11

Author Comment

ID: 35099664
I now have add'l info. I think this has nothing to do with jquery, but a behavior of javascript that I was never aware of. If I put 2 javascript blocks inside an html <head> block, the 1st js block gets ignored and the 2nd one gets executed. Like so:


<script language="javascript" type="text/javascript">
   alert("alert #1")

<script language="javascript" type="text/javascript">
   alert("alert #2")


Open in new window

Load the page, and you will only see "alert #2". Can someone confirm this behavior is real? The reason I have 2 js blocks in the head and not just 1 is because the 1st one is part of my master page, and the 2nd is reserved for individual pages. The master page gets merged at runtime with individual pages (like an include/header idea), so both js blocks end up on the page together.

I am using IE8.

Any ideas?
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

LVL 11

Author Comment

ID: 35099764
I just tried placing both js blocks in the body tag instead of head: same behavior. First js block gets ignored.

This can't be; I know I've seen web pages with multiple js blocks and they all execute.

Note: In addition to my 2 blocks near the top of body, there is the 3rd block further down containing all the jquery stuff.
LVL 82

Accepted Solution

hielo earned 500 total points
ID: 35101523
>> I think this has nothing to do with jquery,

>>but a behavior of javascript that I was never aware of. I put 2 javascript blocks inside an html <head> block, the 1st js block gets ignored and the 2nd one gets executed.
That is a problems specific to YOUR environment. It has nothing to do with javascript in general. I can confirm that the code snippet you provided correctly alerts BOTH statements.

I am not an developer, but my understanding is that you need to "register" (whatever that means) the scripts that you add, otherwise they will not be executed.
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35106202
I guess that is not exactly the environmental cause - the asp's (which I do not know, too) javascript is somehow blocking the other ones below. Perhaps you can paste your code and additionally ask to move the question to asp section, where some asp expert can help further.
LVL 11

Assisted Solution

SweatCoder earned 0 total points
ID: 35157253
I found the problem. I was doing this:

<script type="text/javascript" src="Scripts/Resize.js" />

...which doesn't work. This does work:

<script type="text/javascript" src="Scripts/Resize.js"></script>

That was the problem.
LVL 11

Author Closing Comment

ID: 35187293
Accepting my own solution, because that's what fixed it. Also accepting hielo.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
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…

749 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