Developer forum

Forum » Development » Insert link to javascript file in the head

Insert link to javascript file in the head

Rob Lohmann
Reply
I'm building a notificationsubscriber which I want to use to insert a piece of javascript into the headsection of the masterpage.
In order to do this i'm using the piece of code below;

Public Overrides Sub OnNotify(ByVal notification As String, ByVal args As Object())
            Dim pageView As PageView = TryCast(args(0), PageView)

            pageView.AddJavascript("/Scripts/cookie-settings.js")
            Dim res = pageView.getJavascripts().Insert(0, "/Scripts/cookie-settings.js")
            pageView.Current.AddJavascript("/Scripts/cookie-settings.js")

            If Helper.CookieExists("cc_cat2") Then

            End If
        End Sub


The code executes, but I don't see any javascript being outputted to the headsection of the page. What is the correct way to do this? I've also tried adding an absolute path to the javascript instead of a relative one, but this doesn't work either.

I'm expecting something like this in the headsection;

<script src="/Scripts/cookie-settings.js" type="text/javascript"></script>



Replies

 
Vladimir
Reply
This post has been marked as an answer
Hi Rob,
try to sibscribe on
Dynamicweb.Notifications.Standard.Page.Loaded
or
Dynamicweb.Notifications.Standard.Page.OnGlobalTags

    <Extensibility.Subscribe(Global.Dynamicweb.Notifications.Standard.Page.OnGlobalTags)> _
    Public Class PageviewObserver
        Inherits Global.Dynamicweb.Extensibility.NotificationSubscriber

        Public Overrides Sub OnNotify(ByVal notification As String, ByVal args As Global.Dynamicweb.Extensibility.NotificationArgs)
            If args Is Nothing Then
                Return
            End If

            Dim pna As Global.Dynamicweb.Notifications.Standard.Page.PageviewNotificationArgs = DirectCast(args, Global.Dynamicweb.Notifications.Standard.Page.PageviewNotificationArgs)
            pna.Pageview.AddJavascript("myscript.js")

        End Sub
    End Class


Best regards,
Vladimir

Votes for this answer: 0
 
Rob Lohmann
Reply
Hi Vladimir,

Thanks for your response, will try this and let you know how it went.

- Rob
 
Rob Lohmann
Reply
Hi Vladimir,

This worked, thanks!

- Rob

 

You must be logged in to post in the forum