StudentRik
StudentRik

Reputation: 1049

How do I call this function with jQuery

Im trying to get the code to run but nothing happens as Im not sure if I have to call the function or does it run automatically.

When using the window.onload method I would give the function a name < init > and it would run. Calling it with jquery im un sure if I should have a function name or not?

how should I get the code to run please.

// JavaScript Document
$(document).ready(function () {
    var xhr = false;
    var xPos, yPos;

    function () {
        var allLinks = document.getElementsByTagName("a");

        for (var i = 0; i < allLinks.length; i++) {
            allLinks[i].onmouseover = showPreview;
        }

    } //end function
    function showPreview(evt) {
        if (evt) {
            var url = evt.target;
        } else {
            evt = window.event;
            var url = evt.srcElement;
        }
        xPos = evt.clientX;
        yPos = evt.clientY;

        if (window.XMLHttpRequest) {
            xhr = new XMLHttpRequest();
        } else {
            if (window.ActiveXObject) {
                try {
                    xhr = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (xhr) {
            xhr.onreadystatechange = showContents;
            xhr.open("GET", url, true);
            xhr.send(null);
        } else {
            alert("Sorry, but I couldn't create an XMLHttpRequest");
        }
        return false;
    }

    function showContents() {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                var outMsg = xhr.responseText;
            } else {
                var outMsg = "There was a problem with the request " + xhr.status;
            }
            var prevWin = document.getElementById('previewWin');
            prevWin.innerHTML = outMsg;
            prevWin.style.top = parseInt(yPos) + 2 + "px";
            prevWin.style.left = parseInt(xPos) + 2 + "px";
            prevWin.style.visibility = "visible";

            preview.onmouseout = function () {
                document.getElementById('preview').style.visibility = "hidden";
            }
        }
    }
});

Upvotes: 0

Views: 89

Answers (3)

Bloafer
Bloafer

Reputation: 1326

You have two options, inline JavaScript:

<script>...yourcode...</script>

Or linked javascript in an external file:

<script type="text/javascript" src="yourfile.js"></script>

usually linked files are in the head tags, inline scripts would be at the bottom.

If you are unsing jQuery you also need to include this library http://jquery.com

function () {
    var allLinks = document.getElementsByTagName("a");

    for (var i = 0; i < allLinks.length; i++) {
        allLinks[i].onmouseover = showPreview;
    }

} //end function

Should really be:

function () {
    var allLinks = $("a");

    allLinks.on("mouseover", showPreview);

} //end function

Upvotes: 0

presci
presci

Reputation: 40

you have declared the functions. There is no where you are calling the functions so that it executes.

You won't be able to call this functions from outside as they are declared inside the document.ready function. Do something like this

$(document).ready(function() {
    function showcontent(){
    };
    showcontent();
});

Upvotes: 2

Wouter J
Wouter J

Reputation: 41934

jQuery === JavaScript

So calling a function in jQuery is exact the same as calling a function in JavaScript.

The only thing jQuery does is create easy-to-use methods and functions to speed up your JavaScript development. For example, the window.onload event is replaced by a $(document).ready() event.

Upvotes: 1

Related Questions