修改javascript以在新窗口中打开(window.location.href)

时间:2016-11-13 17:20:58

标签: javascript

我为需要使用邮政编码搜索提供商的客户创建网站,但搜索位于不在我们域内的提供商搜索网站上。我需要修改此脚本才能强制它在新窗口中打开。

<script type="text/javascript">
    $(document).ready(function () {
        var submitVetSearchD = function () {
            submitVetSearch($('#zip'));
        }

        function submitVetSearchM() {
            submitVetSearch($('#zipM'));
        }

        function submitVetSearchF() {
            submitVetSearch($('#zipF'));
        }

        function submitVetSearch(ele) {
            var zipval = ele.val();
            var url = 'https://www.fakesearchresultswebsite.com/';
            if (zipval !== "") {
                return window.location.href = url + '?zip=' + zipval;
            }
        }

        $("#zip").keyup(function (event) {
            if (event.keyCode == 13) {
                submitVetSearchD();
            }
        });
        $('#large-header-vet-search').click(submitVetSearchD);

        $("#zipM").keyup(function (event) {
            if (event.keyCode == 13) {
                submitVetSearchM();
            }
        });
        $('#mob-vet-search').click(submitVetSearchM);

        $("#zipF").keyup(function (event) {
            if (event.keyCode == 13) {
                submitVetSearchF();
            }
        });
        $('#footer-vet-search').click(submitVetSearchF);

        $("a[rel^='prettyPhoto']").prettyPhoto();

        //top menu size
        $(window).resize(function () {
            setTopMenuHeight();
        });

        setTopMenuHeight();

        var smallHeader = false;
        var menuBig = true;

        $(document).scroll(function () {
            var ele = $("#top-menu-height");
            var menuBigNew = !(ele.is(":visible") && $(window).scrollTop() > 200);

            if (menuBig != menuBigNew && smallHeader) {
                menuBig = menuBigNew;

                $("#navtop").toggleClass("small-page-header");
                $(".fixed-top").toggleClass("pos-fix");
                $("#page-header").toggleClass("fixed-header");
                $("#title-area").toggleClass("title-area-small");

                $("#logo1").finish();
                $("#logo2").finish();
                $("#top-menu").finish();

                if (menuBig) {
                    $("#logo2").toggle();
                    $("#logo1").toggle(1000);
                } else {
                    $("#logo1").toggle();
                    $("#logo2").toggle(1000);
                }

                //$("#top-menu").fadeToggle();
            }
        });

        //$('body').on('open.fndtn.reveal', function(){
        //    $('body').css('overflow', 'hidden');
        //});

        //$('body').on('closed.fndtn.reveal', function(){
        //    $('body').css('overflow', 'visible');
        //});
    });

    function setTopMenuHeight() {
        var ele = $("#top-menu-height");
        var height = $("#page-header").height();
        ele.height(height);
    }
</script>

1 个答案:

答案 0 :(得分:1)

替换

return window.location.href = url + '?zip=' + zipval;

var newWin = window.open(url + '?zip=' + zipval);
return true;

在全局范围内声明newWin,它将引用新窗口。

参考:http://www.w3schools.com/jsref/met_win_open.asp