Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


jquery colliding with my javascript

Posted on 2011-03-10
Medium Priority
Last Modified: 2012-05-11
I have some jquery inside an asp.net user control on my asp.net 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 asp.net 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) {
                    divMainHandle.style.height = (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 asp.net 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?

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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 2000 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 ASP.net 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

Industry Leaders: 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

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
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 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)
Suggested Courses

571 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