Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If not declared, declare the variable?

Tags:

javascript

The topic might be a bit cryptic, but here's my problem (perhaps a n00b-thing?):

For my site I have a widget, that user can use on their site as such (for example):

<script type="text/javascript">
    var widget_width ="300";
</script>
<script src="http://something/a.js" type="text/javascript"></script>

Now, in the .js file I want to check if the user have declared the widget_width variable. So I thought I might do something like (I want the variable to be empty, "", because I do a server-side validation too):

if(typeof widget_width == 'undefined') {
    var widget_width = "";
}

This didn't work, neither did:

if(!widget_width) {
    var widget_width = "";
} 

Any ideas on how do declare a variable when needed this way?

like image 409
Marcus Olsson Avatar asked Apr 28 '26 09:04

Marcus Olsson


1 Answers

remove the var. make it >

if(typeof widget_width == 'undefined') {
    widget_width = "";
}
like image 198
neebz Avatar answered Apr 30 '26 23:04

neebz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!