开发者

How can i seperate jQuery code from my html markup

Am using php to develop an app and i created a simple template system for headers and footers. My dilemma is that i have many views that depend on the jQuery code, and they all user the header template. Here an excerpt from the header.

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /开发者_运维百科>
<title>NgInformr | Adminstration | <?php if(isset($title)) { echo $title; }?></title>
<?=$html->js("javascripts/top_up")?>
<?=$html->css("admin-css")?>
<?=$html->css("form-style")?>
<link href="http://localhost/nginformr/www_root/js/uploadify/uploadify.css" type="text/css" rel="stylesheet"/>
<?=$html->css("base/jquery.ui.all")?>
<?=$html->js("uploadify/jquery.uploadify.v2.1.4.min")?>
<?=$html->js("uploadify/swfobject")?>
<?=$html->js("jquery-ui-1.8.9")?>
<?=$html->js("jscripts/tiny_mce/jquery.tinymce")?>
<script type="text/javascript">
$(document).ready(function() {
//Uploader
$('#file_upload').uploadify({
    uploader : "/nginformr/www_root/js/uploadify/uploadify.swf",
    script  : "/nginformr/www_root/js/uploadify/uploadify.php",
    folder : "/nginformr/www_root/uploads",
    auto : true,
    onComplete : function(event, queueID, fileObj, reponse, data) {
        $('#file_details').css({display:"block"});
        $('#path').val(fileObj.filePath);
        $('#type').val(fileObj.type);
    },
    onError    : function (event,ID,fileObj,errorObj) {
      alert(errorObj.type + ' Error: ' + errorObj.info);
    }
});
$("#side_menu").accordion({header:"p",fillSpace: false});
//Ajax for select boxes
$('#state').change(function() {
        $('#city').html("");
        var id = $('#state').val();
        var dataString = "id="+id;
    $.ajax({
        url:"http://localhost/nginformr/cities/ajax_cities",
        type:"POST",
        data:dataString,
        success:function(html) {
            $('#city').append(html);
        }
    });
});
$("#city").change(function() {
    $('#area').html("");
    var id = $('#city').val();
    var dataString = "id="+id;
    $.ajax({
        url:"http://localhost/nginformr/areas/ajax_areas",
        type:"post",
        data:dataString,
        success:function(html) {
            $('#area').append(html);
        }
    });
});
$('#sector').change(function() {
    $('#category').html("");
    var id = $('#sector').val();
    var dataString = "id="+id;
    $.ajax({
        url:"http://localhost/nginformr/categories/ajax_categories",
        type:"post",
        data:dataString,
        success:function(html) {
            $('#category').append(html);
        }
    });
});
$(".widget").draggable();

//TinyMCE Editor
$('textarea.tinymce').tinymce({
    // Location of TinyMCE script
    script_url : 'http://localhost/nginformr/www_root/js/jscripts/tiny_mce/tiny_mce.js',

    // General options
    theme : "advanced",
    plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist",

    // Theme options
    theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
    theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
    theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
    theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",
    theme_advanced_statusbar_location : "bottom",
    theme_advanced_resizing : true,

    // Example content CSS (should be your site CSS)
    //content_css : "css/content.css",

    // Drop lists for link/image/media/template dialogs
    template_external_list_url : "lists/template_list.js",
    external_link_list_url : "lists/link_list.js",
    external_image_list_url : "lists/image_list.js",
    media_external_list_url : "lists/media_list.js",

    // Replace values for the template plugin
    template_replace_values : {
        username : "Some User",
        staffid : "991234"
    }
});
});
</script>
<script type="text/javascript">
  TopUp.host = "http://localhost/nginformr/";
  TopUp.images_path = "www_root/img/top_up/";
</script>
</head>
<body>
<div id="header"><!-- ------Header -->
<div id="logo">
<a href="./"><?$html->img("nginformr_logo.png") ?></a>
</div>

<div id="user_controls">
<?=$html->link("admin/users/logout","Logout")?>
</div>
</div>


If you mean to move your jQuery code out of your header and into a separate file, you would simply copy and paste it all (minus the HTML script tags) into a javascript document in your site's folder (eg general.js).

Then simply reference that file from the HTML in the same manner you referenced the other javascript files.

<?=$html->js("general.js")?>


Why don't you just create a Javascript file and include it in your HTML ?

So copy paste all your JQuery code into (for example) jquery_functions.js and include it:

<html>
    <head>
        ...
        <script type="text/javascript" language="javascript" src="/javascript/jquery_functions.js"></script>
        ...
    </head>
    <body>
        ...
    </body>
</html>

Or by using the function you are using in your code example

<?=$html->js("/javascript/jquery_functions")?>


I'm not sure why you don't put your jQuery code into its own js file and have your template include it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜