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

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?
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

809 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