javascript Laravel 4-重定向到javascript代码刀片模板中的路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23218822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 00:33:41  来源:igfitidea点击:

Laravel 4- redirect to a route inside javascript code Blade template

javascriptphpredirectlaravel-4blade

提问by BaHar Ay?ub

I'm trying to write a simple redirection using javascript and blade engine, and this is an example of what i want to do:

我正在尝试使用 javascript 和刀片引擎编写一个简单的重定向,这是我想要做的一个例子:

javascript code:

javascript代码:

<p>Click the button to go to the home page</p>

<button onclick="myFunction()">Click me</button>

<p id="demo"></p>

<script>
function myFunction()
{
    var x;
    var r=confirm("Are you sure you want to leave this page ?");
    if (r==true)
    {
      //Redirect to home
    }
    else
    {
     //stay here
    }
    document.getElementById("demo").innerHTML=x;
}
</script>

when i use return Redirect::route('home')it does not work, what is the right syntax to do that, and sorry for my bad english.

当我使用 return 时Redirect::route('home')它不起作用,什么是正确的语法来做到这一点,并为我的英语不好而感到抱歉。

回答by Павел Иванов

$url=URL::to('foo'); // any other route name, like home, or use URL::route()

$url=URL::to('foo'); // 任何其他路由名称,例如 home,或使用 URL::route()

<script>
function myFunction()
{
   var x;
   var r=confirm("Are you sure you want to leave this page ?");
   if (r)
   {
      window.location="{{URL::to('home')}}";
   }
...

More info: http://laravel.com/docs/routing

更多信息:http: //laravel.com/docs/routing