开发者

JavaScript equivalent of this simple php

please can somebody let me know how to do the following PHP in JavaScript. The vaibles in PHP are Parameters in Javascript. If all three Parameters are set then I would like a PHP script to execute:

<?php     
if isset($nation,$service,$slider)
    {
//execute php script
    }       
?>

Thanks for looking, and maybe helping!

To clear any confusion, I require all the PHP above to be converted into it JavaScript equivalent.

On top of th开发者_运维技巧is I would like a way of executing a PHP script within the JavaScript if all 3 Parameters are set.


Assuming none of your variables have a zero or null value, for example, if they're all strings, the shortest way to write variable checks is to just use them:

function blah (nation, service, slider)
{
    if (nation && service && slider)
    {
        // do something...
    }
}

If any of those vars have the value 0, false or null when set then this would not be the correct method. If you want the equivalent of the isset function, you could use this:

function isset()
{
    for (var i=0, l=arguments.length; i < l; i++)
    {
        if (typeof arguments[i] == "undefined" || typeof arguments[i] == "null")
            return false;
    }
    return true;
}

Use it the same as you would the php function, e.g. isset(nation,service,slider);


Check out the great phpjs.org stuff. It's a port of PHP functions into JavaScript.

The isset() function:

function isset () {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: FremyCompany
    // +   improved by: Onno Marsman
    // *     example 1: isset( undefined, true);
    // *     returns 1: false
    // *     example 2: isset( 'Kevin van Zonneveld' );
    // *     returns 2: true

    var a=arguments, l=a.length, i=0;

    if (l===0) {
        throw new Error('Empty isset'); 
    }

    while (i!==l) {
        if (typeof(a[i])=='undefined' || a[i]===null) { 
            return false; 
        } else { 
            i++; 
        }
    }
    return true;
}

Usage:

allVarsAvailable = isset(nation,service,slider);


Haven't tested, but looks right:

function isset(variable_name){
  return(typeof(window[variable_name])!='undefined');
}

Note that variable_name is not a variable itself, but it's name.


From what I understand you are trying to do an AJAX call. If you were to use a framework like jQuery, the code would be something like:

if ((typeof nation != 'undefined') && (typeof service != 'undefined') && (typeof slider != 'undefined')) {
    $.get("<your script>", function(data){
         alert("Data Received: " + data);
    });
}

If this is not what you are looking for, please rephrase your question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜