What causes the Object expected error in jQuery?

Looking for a way to demo horizontal collapse pane with a simple web page that includes html, css, and jquery.

<html>

<head>
    <script type="text/javascript" src="//code.jquery.com/jquery-1.10.1.js"></script>
    <title>Sample HTML</title>
    <style type="text/css">
        html, body {
            width: 100%;
            height: 100%;
        }

        #map {
            width: 10%;
            height: 100%;
            float: left;
        }

        #sidebar {
            width: 89%;
            height: 100%;
            float: left;
            border: 1px solid;
        }

        #toggle {
            height: 10%;
            float: right;
        }
    </style>
    <script type="text/javascript">
        $(window).load(function () {//this is error line

            $(document).ready(function () {
                $("#toggle").click(function () {
                    if ($(this).data('name') == 'show') {
                        $("#sidebar").animate({
                            width: '10%'
                        }).hide()
                        $("#map").animate({
                            width: '89%'
                        });
                        $(this).data('name', 'hide')
                    } else {
                        $("#sidebar").animate({
                            width: '89%'
                        }).show()
                        $("#map").animate({
                            width: '10%'
                        });
                        $(this).data('name', 'show')
                    }
                });
            });
        });
    </script>


    </head>
<body>

    <div id="map">
        <input type="button" data-name="show" value="Toggle" id="toggle"></div>
    <div id="sidebar">SIDEBAR</div>
</body>
</html>

Struggling with an issue in IE debugger where it hits at $(window).load(function () with an "Object expected" error message. Any ideas on why this isn't working?

Additionally, noticing that this page is loading quite slowly.

Answer №1

Avoid using $(window).load(function () { because $(document).ready is sufficient. The document.ready event triggers when the DOM is ready, while window.load waits for all resources to finish loading. For more details, refer to this link.

If you're encountering an error, it may be due to an incorrect jQuery library path. Double-check the path as shown below:

<script type="text/javascript" src="//code.jquery.com/jquery-1.10.1.js"></script>

The path should ideally start with http://:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.js"></script>

Once you've corrected the library path, removing window.load should resolve the issue.

<script type="text/javascript">
    $(document).ready(function () {
        $("#toggle").click(function () {
            if ($(this).data('name') == 'show') {
                $("#sidebar").animate({
                    width: '10%'
                }).hide()
                $("#map").animate({
                    width: '89%'
                });
                $(this).data('name', 'hide')
            } else {
                $("#sidebar").animate({
                    width: '89%'
                }).show()
                $("#map").animate({
                    width: '10%'
                });
                $(this).data('name', 'show')
            }
        });
    });
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Angular 2: Testing Firebase Add Functionality with Unit Tests

Is there a way to perform a basic unit test in Angular 2 for testing a simple firebase adding item feature? I've opted for typescript over standard JavaScript in my code. This is the piece of code I want to test: export class AppComponent { r ...

Unable to establish a breakpoint in a source-mapped file (illustrated using jQuery)

Having trouble setting a breakpoint on a minified JavaScript source file that is mapped to real sources using a source map file. An example of this problem can be seen on the website jquery.com. On this particular site, the imported script is jquery.min. ...

Obtaining Output from within the $.ajax() Method

Is it possible to retrieve a value from within an $.ajax function in JavaScript? Here's the structure I'm working with: function getValue(){ var number = 0; $.ajax({ 'url':'/some/url', 'type':&apo ...

The onclick event is malfunctioning in Chrome when dealing with data driven by SQL

<select id='city' name='city' > <?php $dbcon = mysql_connect($host, $username, $password); mysql_select_db($db_name,$dbcon) or die( "Unable to select database"); $city_query = "SELECT city,county FROM citycatalog order by city ...

How can I compare the current page URL with the navigation bar?

Looking to compare the URL of a current page to an element in the navigation bar using jQuery. Started by assigning the current URL to a variable: var currentPage = window.location.href; Then utilized an .each function to loop through each item in the n ...

Utilizing itemprop tag independent of itemtype declaration

My question is whether it's possible to use the itemprop attribute without using the itemtype. Can this be done? If so, how does using these attributes impact SEO? Is it recommended to always use itemtype? I assume it would be beneficial, but what if ...

Having trouble with exporting static HTML using Next.js

Recently, I have completed a project and now I am looking to export it to static HTML. In order to achieve this, I added the following command in my package.json file: build" : "next build && next export Upon running the command npm run ...

The Nuxt auth module fails to update the user's loggedin status within the store state

Currently, I am implementing Authentication functionality using the Nuxt Auth Module. My frontend is built on Nuxt Js, while my backend is running Laravel 5.7 In nuxt.config.js, I have configured the auth settings as follows: auth: { strategies: { ...

Getting the AJAX response back to the main page

I'm currently working on a user registration form within a jQuery modal dialog. Once the form is filled out and submitted, the data is successfully sent to a MySQL database using jQuery/Ajax. However, my challenge lies in displaying the updated data ...

Mastering the art of Promises and handling errors

I've been tasked with developing a WebApp using Angular, but I'm facing a challenge as the project involves Typescript and asynchronous programming, which are new to me. The prototype already exists, and it includes a handshake process that consi ...

Panel floating with Bootstrap framework

I have created a unique two-column layout using Bootstrap, utilizing the col-md-6 class. The layout consists of a row with a panel at the top containing a table, a left column panel displaying a list of media items, and a right column panel containing text ...

Interact with various contenteditable sections by navigating with the arrow keys

I have a challenge with multiple <p contenteditable="true"></p> elements on my page. I am seeking a solution to enable the use of arrow keys to navigate seamlessly across these separate elements as if they were one cohesive editable element. F ...

Is it possible to turn a <span> element into a clickable hyperlink?

Can a span be turned into a clickable link? I have successfully made a span with only a background image (using the Gilder/Levin image replacement technique) into a clickable link. This seems to work well on my desktop in Chrome, Opera, and IE 11. Is thi ...

Axios - retrieving merchandise

I have created an endpoint that retrieves all product information in JSON format. I am attempting to display this data on my index.html page using Axios, but I am encountering difficulties with getting the data correctly. This is my first time working with ...

Having trouble navigating through multiple layers of nested array data in react js

I need help understanding how to efficiently map multiple nested arrays of data in a React component and then display them in a table. The table should present the following details from each collection: title, location, description, and keywords. Below ...

"Encountering a Dilemma in Resolving State using

Struggling to implement ui router for setting states post fetching pages from a database, I encountered an error each time I tried clicking on a sref-link. Below are snippets from app/app.js and app/index.html. angular.js:10467 Error: Could not resolve &a ...

Having a slight hiccup with pixel alignment and browser compatibility in my jQuery animation

To emphasize a specific paragraph element, I developed a JavaScript function that displays it above a darkened background. My approach involved using jQuery to generate an overlay and then duplicating the targeted paragraph element while positioning it ab ...

No data is being returned by the Jquery Ajax function

I am experiencing an issue with a Jquery Ajax call in my code: $('body').on('click', '#btnPopulate', function() { alert(getTree()); }); function getTree() { var url = getUrlPath() + "/StoryboardAdmin/BuildStoryboardViewMode ...

Creating synchronicity in your code within the useEffect hook

Is there a way to ensure that my function is fully completed before moving on, even though it's not recommended to add async to useEffect? Take a look at this code snippet: useEffect( () => { const RetrieverDataProcess = async () => ...

Loading modules conditionally in Nuxt.js

In my Nuxt.js configuration, I have included a module for Google Tag Manager like this: modules: [ [ '@nuxtjs/google-tag-manager', { id: 'GTM-XXXXXXX' } ] ] Everything is functioning properly, but I am curious ab ...