Solved

jquery colliding with my javascript

Posted on 2011-03-10
8
455 Views
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 () {
            resizeDiv();
        }

        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?
0
Comment
Question by:SweatCoder
8 Comments
 
LVL 7

Expert Comment

by:dxdinh
ID: 35098576
change your $(document).ready

jQuery(document).ready
0
 
LVL 11

Author Comment

by:SweatCoder
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.
0
 
LVL 11

Author Comment

by:SweatCoder
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:

<head>

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

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

</head>

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?
0
 
LVL 11

Author Comment

by:SweatCoder
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.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 82

Accepted Solution

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

>>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.
0
 
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.
0
 
LVL 11

Assisted Solution

by:SweatCoder
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.
0
 
LVL 11

Author Closing Comment

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
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…

746 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

11 Experts available now in Live!

Get 1:1 Help Now