Solved

jquery colliding with my javascript

Posted on 2011-03-10
8
464 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
[X]
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
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
Understanding Linux Permissions

Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.

 
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
 
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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

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…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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)
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…

636 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