1

All the pages in my website has the same header. I implemented that like this:

extends('layouts.main')

Now in some pages, I need to send a successful message and in other pages not. I implemented that like this:

return....->with(array(
sucessfulMessage => 'asdf'
));

Please note that I do that just in some pages not in all pages.

Now I want to use that variable in JavaScript. I did this in the layout.main:

<head>
    {{ HTML::script('/js/jquery-2.1.1.js')}}
    @if(isset($successfulMessage))
    <script>
        var successfulMessage = "{{$successfulMessage}}";
    </script>
    @endif
    {{ HTML::script('/js/myPopupScript.js')}}
</head>

and then in myPopupScript.js I did this:

$(document).ready(function(){
    if(!typeof successfulMessage === 'undefined'){
        alert(successfulMessage);
    }else{
        alert("test");
    }
});

My problem

The alert is always test even though when the successfulMessage has been sent.

Important note

When I use F12 in Google Chrome to see the actual HTML, I can see this:

<script>
        var successfulMessage = "asdf";
    </script>

so the variable is defined.

19
  • !typeof successfulMessage === 'undefined' = (!(typeof sucessfullMessage)) === 'undefined' = false === 'undefined' = false. Commented Jul 31, 2014 at 19:58
  • @Cthulhu couldn't understand your comment, could you explain please? Commented Jul 31, 2014 at 20:01
  • Just saying that if correct operators precedence taken into account, your condition will never hold. What you wanted is typeof successfulMessage !== 'undefined'. Commented Jul 31, 2014 at 20:03
  • @Cthulhu you right, because I tried to define the variable before the condition and still not working. that a good notice, please how to solve it? Commented Jul 31, 2014 at 20:04
  • The real problem here is that when you try to access the variable successfulMessage never has assigned the php value, remember php executes is server-side and the script load at first. Commented Jul 31, 2014 at 20:07

2 Answers 2

1

The code that works for me is this:

$(document).ready(function(){
    var typeOfSuccessfulMessage = typeof successfulMessage;

    if(typeOfSuccessfulMessage == 'string'){
       alert(successfulMessage);
    }else{
        alert("undefined");
    }
});

Many thanks for @martinezjc, @lowerends and @Cthulhu

Sign up to request clarification or add additional context in comments.

1 Comment

@martinezjc I need two day to accept my own answer :) but truly I appreciate your help
0

Change this piece of code:

Edit:

Try to change the condition in your if/else javascript block

$(document).ready(function() {
     @if(isset($successfulMessage))
        var successfulMessage = {{$successfulMessage}};
    @endif

    if( !typeof successfulMessage === 'undefined' ) {
        alert("test");
    } else {
        alert(successfulMessage);
    }
});

Hope this works :)

3 Comments

your new code works just when the variable is defined, but where there is no variable, I got this error message: Uncaught ReferenceError: successfulMessage is not defined
your second new code has a syntax error, can't write @ inside a jquery function
I checked and told you. can't' write @ in a jquery

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.