简体   繁体   中英

javascript function only works with the first included function

I have the following function ( onButtonClick ) when the button is clicked only the first function works. All the included functions work if I only have one of them in the button function.

I have also tried the on button click function this way as well. Still does not work.

setProjectData() && getProjectData() && window.location.href='support_page.html';

Any ideas on what I may be missing?

<span class="profile_container_right">
    <a onclick='onButtonClick()'; return true;"; ><img src="images/bSUPPORT.png"/></a>
</span>

<script>
    /* on button click */           
    function onButtonClick(){
        setProjectData();
        getProjectData();
        window.location.href='support_page.html';
    }

    function setProjectData() {
    // --- set local storage project descriptions ---
    vProjectName=‘The Project Name’;
            localStorage.setItem('xProjectName', vProjectName);
            return true;
    }

    function getProjectData() {
    // --- get local storage project descriptions ---
            vProjectName = localStorage.getItem('xProjectName');
            alert('you entered the GET project data function ='+vProjectName);
            return true;    
    }
</script>

First issue is the use of quotes in the onclick binding:

onclick='onButtonClick()'; return true;";

The correct syntax for onclick is onclick="yourFunctionCall();" for example:

onclick="onButtonClick(); return true;"

Second issue is your use of quotes in the setProjectData function:

vProjectName=‘The Project Name’;

Change to single quotes or double quotes:

vProjectName="The Project Name";

With these syntax issues fixed your code should execute as expected.

Side note use var to declare variables within a function to make them adhere to the scope of the function. Declaring a js variable without the var exposes the variable globally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM