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.
!typeof successfulMessage === 'undefined'=(!(typeof sucessfullMessage)) === 'undefined'=false === 'undefined'=false.typeof successfulMessage !== 'undefined'.successfulMessagenever has assigned the php value, remember php executes is server-side and the script load at first.