locked
javascript endpoint in head section RRS feed

  • Question

  • User-125499312 posted

    i am using square credit card processing

    for testing

    <head id="head1" runat="server">
    <script type="text/javascript" src="https://js.squareup.com/v2/paymentform"></script>
    </head>

    for live use

    <head id="head1" runat="server">
    <script type="text/javascript" src="https://js.squareupsandbox.com/v2/paymentform"></script>
    </head>

    i would like to conditionally load one file 

    if testing

    <script type="text/javascript" src="https://js.squareup.com/v2/paymentform"></script>

    else

    <script type="text/javascript" src="https://js.squareupsandbox.com/v2/paymentform"></script>

    end if

    how would i do this in javascript in the head section

    i have already tried 

    Dim lcSquareJS As New LiteralControl(vbCrLf + "<script type=" + Chr(34) + "text/javascript" + Chr(34) + " src=" + Chr(34) + "https://js.squareup" + strSquareEnvironment + "/v2/paymentform" + Chr(34) + "></script>" + vbCrLf)
    Me.head1.Controls.AddAt(1, lcSquareJS)

    and it did not work

    thx for ur help

    Tuesday, June 23, 2020 8:13 PM

Answers

  • User-939850651 posted

    Hi yzidell,

    Based on your description, I think ClientScriptManager.RegisterClientScriptInclude Method may help you achieve your needs.

    You can add it to the header in the Page_Load event, just like this:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Page.ClientScript.RegisterClientScriptInclude("script", "https://js.squareup.com/v2/paymentform")
    End Sub

    And an empty page named “AddJsFile” and its result:

    You also can choose which file to import after judging the operation.

    Hope this can help you.

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 24, 2020 9:40 AM

All replies

  • User409696431 posted

    In page load, use whatever test you have for the environment and add the correct javascript to the head section.  I'm assuming localhost uses the sandbox javascript.  Make sure the <head> tag has the runat="server" attribute included.

    Something like this:

    Protected Sub Page_Load(sender As Object, e As EventArgs)
       If Not Request.Url.ToString.Contains("://localhost") Then
         Dim javascriptRef As New LiteralControl("<script type='text/javascript' src='https://js.squareup.com/v2/paymentform'></script>")
       Else
          Dim javascriptRef As New LiteralControl("<script type='text/javascript' src='https://js.squareupsandbox.com/v2/paymentform'></script>")
       End If
       Page.Header.Controls.Add(javascriptRef) 
    End Sub

    If this page posts back to itself, include a test for it not being a postback in the above code.  (if ispostback = false then...)

    Wednesday, June 24, 2020 5:08 AM
  • User-939850651 posted

    Hi yzidell,

    Based on your description, I think ClientScriptManager.RegisterClientScriptInclude Method may help you achieve your needs.

    You can add it to the header in the Page_Load event, just like this:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Page.ClientScript.RegisterClientScriptInclude("script", "https://js.squareup.com/v2/paymentform")
    End Sub

    And an empty page named “AddJsFile” and its result:

    You also can choose which file to import after judging the operation.

    Hope this can help you.

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 24, 2020 9:40 AM
  • User-125499312 posted

    thanks - worked like a charm

    Wednesday, June 24, 2020 7:50 PM