我怎么能通过一个变量与url在javascript代码

How can i pass a variable with url in javascript code?

本文关键字:变量 url 代码 javascript 一个 怎么能      更新时间:2023-09-26

我想在JavaScript代码中传递一个URL变量。变量来自数据库。我在StackOverflow上看到了很多答案。但我想,我找不到任何正确的答案来解决我的问题。代码如下:

这段代码在我的索引页。

 <script type="text/javascript">
          $(document).ready(function(){
            $("#btn-view").hide();
            $("#btn-add").click(function(){
                $(".content-loader").fadeOut('slow', function()
                {
                    $(".content-loader").fadeIn('slow');
                    **$(".content-loader").load('video_add.php?lang=$lang')**;
                    $("#btn-add").hide();
                    $("#btn-view").show();
                });
            });
            $("#btn-view").click(function(){
                $("body").fadeOut('slow', function()
                {
                    **$("body").load('video.php?lang=$lang');**
                    $("body").fadeIn('slow');
                    **window.location.href="video.php?lang=$lang";**
                });
            });
        });
    </script>

此代码在code.js文件中。

// JavaScript Document
$(document).ready(function(){
/* Data Insert Starts Here */
$(document).on('submit', '#emp-SaveForm', function() {
   $.post("video_create.php?lang=$lang", $(this).serialize())
    .done(function(data){
        $("#dis").fadeOut();
        $("#dis").fadeIn('slow', function(){
             $("#dis").html('<div class="alert alert-info">'+data+'</div>');
             $("#emp-SaveForm")[0].reset();
         });    
     });   
     return false;
});
/* Data Insert Ends Here */

/* Data Delete Starts Here */
$(".delete-link").click(function()
{
    var id = $(this).attr("id");
    var del_id = id;
    var parent = $(this).parent("td").parent("tr");
    if(confirm('Sure to Delete ID no = ' +del_id))
    {
        $.post('video_delete.php?lang=$lang', {'del_id':del_id}, function(data)
        {
            parent.fadeOut('slow');
        }); 
    }
    return false;
});
/* Data Delete Ends Here */
/* Get Edit ID  */
$(".edit-link").click(function()
{
    var id = $(this).attr("id");
    var edit_id = id;
    if(confirm('Sure to Edit ID no = ' +edit_id))
    {
        $(".content-loader").fadeOut('slow', function()
         {
            $(".content-loader").fadeIn('slow');
            $(".content-loader").load('video_edit.php?edit_id='+edit_id);
            $("#btn-add").hide();
            $("#btn-view").show();
        });
    }
    return false;
});
/* Get Edit ID  */
/* Update Record  */
$(document).on('submit', '#emp-UpdateForm', function() {
   $.post("video_update.php?lang=$lang", $(this).serialize())
    .done(function(data){
        $("#dis").fadeOut();
        $("#dis").fadeIn('slow', function(){
             $("#dis").html('<div class="alert alert-info">'+data+'</div>');
             $("#emp-UpdateForm")[0].reset();
             $("body").fadeOut('slow', function()
             {
                $("body").fadeOut('slow');
                window.location.href="video.php?lang=$lang";
             });                 
         });    
    });   
    return false;
});
/* Update Record  */
});

所有URL传递一个变量

lang = $朗

。$lang是en或bn,但它来自数据库表。我怎样才能使它可用?

你可以在你的索引页设置javascript数组。

注意:将此脚本放在包含code.js文件

之前
<script>
var global_var = {
lang: "your value",
};
</script>
现在在你的code.js文件
'video_delete.php?lang='+global_var.lang

如果你想传递数据到url,你不使用内联JS那么你可以声明全局变量,然后在*. JS文件中使用它。

交货:这段代码在我的索引页。

<script type="text/javascript">
// Decl Global variable 
var $lang = "<?php print $lang; ?>";
// HEre your normal js code
</script>

此代码在code.js文件中。

 // js code 
 $.post("video_update.php?lang="+$lang, $(this).serialize())
 // Js code