ASP.NET javascript OnClick事件仅适用于第一页加载

时间:2014-07-14 04:14:23

标签: javascript asp.net onclick master-pages

在我的母版页中,我引用了一个脚本:

    <asp:PlaceHolder runat="server">
       <%: Scripts.Render("~/bundles/modernizr") %>

       <script>


var html5_audiotypes={ //define list of audio file extensions and their associated audio types. Add to it if your specified audio file isn't on this list:
    "mp3": "audio/mpeg",
    "mp4": "audio/mp4",
    "ogg": "audio/ogg",
    "wav": "audio/wav"
}

function createsoundbite(sound){
    var html5audio=document.createElement('audio')
    if (html5audio.canPlayType){ //check support for HTML5 audio
        for (var i=0; i<arguments.length; i++){
            var sourceel=document.createElement('source')
            sourceel.setAttribute('src', arguments[i])
            if (arguments[i].match(/\.(\w+)$/i))
                sourceel.setAttribute('type', html5_audiotypes[RegExp.$1])
            html5audio.appendChild(sourceel)
        }
        html5audio.load()
        html5audio.playclip=function(){
            html5audio.pause()
            html5audio.currentTime=0
            html5audio.play()
        }
        return html5audio
    }
    else{
        return {playclip:function(){throw new Error("Your browser doesn't support HTML5 audio unfortunately")}}
    }
}

//Initialize two sound clips with 1 fallback file each:


var clicksound=createsoundbite("click.ogg", "click.mp3")


       </script>
    </asp:PlaceHolder>

然后,我有一个子页面,对于两个图像,我创建了调用该函数的onclick事件:

<asp:HyperLink ID="HyperLink1" runat="server" ImageUrl="~/Images/serviceButton.png" NavigateUrl="~/Users/NewMember.aspx" onclick="clicksound.playclip()"></asp:HyperLink>
        <asp:HyperLink ID="HyperLink2" runat="server" ImageUrl="~/Images/serviceButton.png" NavigateUrl="~/Users/ExistingMember.aspx" onclick="clicksound.playclip()"></asp:HyperLink>        

当我运行这个子页面时,我第一次点击声音播放的其中一个图像并将我带到另一页面。但当我回去时,OnClick事件不再播放了。

编辑:我使用此作为返回默认页面的方式:

<asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl="~/Default.aspx" >Back</asp:LinkButton>

我是否需要将脚本放在其他地方?或者添加一些内容以确保它在页面加载时重新加载?

提前感谢您的帮助!

0 个答案:

没有答案