输入ie8上的调用函数不起作用

时间:2014-04-28 12:48:44

标签: javascript jquery

我的js文件中有一个函数看起来像(见下文) - 我试图在一个按钮上调用一个函数,但它不能在IE8中工作(得到一个"引擎未定义"), FF,Chrome它工作正常: HTML:

<button onclick="javascript:Engine.search.globalSearch(window.event);" type="button" class="btn btn-default">Søg</button>

JS:

jQuery(function ($) {

var $w = $(window),
    $body = $('body'),
    resizeTimer,
    tablePx = 768,
    desktopPx = 992,
    winWidth = $w.width();
Engine = {

    search: {
        setupSearch: function () {
            // Add eventlistener for search-submit on Enter keydown
            $("#q").keydown(function (event) {
                if (event.which === 13) {
                    return Engine.search.submitSearch();
                }
            });

            // Datepicker settings 
            // adding datepicker to date fields from the ASK local Search resume
            // Setting op opensourse Bootstrap datepicker
            $('input.addDatePicker').datepicker({
                format: 'dd.mm.yy'
            });
        },
        globalSearch: function (event) {
            if (event.type === 'click' || event.keyCode === 13 || event.which === 13) {

                // (IE-fix) Prevent default form-submit on Enter-press
                event.preventDefault();
                event.cancelBubble = true;

                // Get search-field and placeholder-tekst
                var searchFld = document.getElementById("globalsearch");
                var ph = searchFld.getAttribute("placeholder");
                if (searchFld.value !== "" && searchFld.value !== ph) {
                    // Get sites-searchpage url, combine it with the searchword and open that url.
                    var searchUrl = searchFld.getAttribute("data-searchUrl");
                    var tmpUrl = searchUrl + "?q=" + searchFld.value;
                    window.location = tmpUrl;
                }
                else {
                    return false;
                }
            }
        },
        autoSumitSearch: function () {
            // Submit search if autoSubmit is setup to true by editor (requires variable defined on SC page)
            if (autoSubmit === true) {
                Engine.search.submitSearch();
            }
        },
        submitSearch: function () {
            // Create outer form with search criteria, to be able to submit it outside Sitecores global form-element
            var $ = jQuery;
            var winLoc = String(window.location.href).replace(/[?&]page[=][^&#]?/, "");
            var form = '<form style="display: none;" method="get" action="' + window.location.href + '">' +
                '<input name="q" value="' + $("#q").val() + '">' +
                //'<input name="page" value="' + $("#page").val() + '">' +
                '<input name="ps" value="' + $("#ps").val() + '">' +
                (($("#categ").length > 0) ? '<input name="categ" value="' + $("#categ").val() + '">' : '') +
                (($("#c").length > 0) ? '<input name="c" value="' + $("#c").val() + '">' : '') +
                (($("#ddlCategories").length > 0) ? '<input name="cat" value="' + $("#ddlCategories").val() + '">' : '') +
                (($("#ddlSiteSections").length > 0) ? '<input name="ss" value="' + $("#ddlSiteSections").val() + '">' : '') +
                (($("#df").length > 0) ? '<input name="df" value="' + $("#df").val() + '">' : '') +
                (($("#dt").length > 0) ? '<input name="dt" value="' + $("#dt").val() + '">' : '') +
                '</form>';

            var $form = $(form).appendTo("body");

            setTimeout(function () { $form.submit().remove(); }, 0);

            return false;
        }
    },
};
// Initialize main script-Engine;
Engine.init();
});

1 个答案:

答案 0 :(得分:0)

可能由于Engine在函数内部使用,因此仅限于该函数的范围。尝试首先全局声明引擎,例如在第一行使用

var Engine;

或使用

在该函数中附加onclick-handler
$("button.your-button").click(Engine.search.globalSearch);

这还将涵盖使用内联onclick处理程序引起的可能冲突,该处理程序在DOMReady事件中调用的方法中声明的事件处理程序。