浏览器不想运行.js

时间:2018-05-31 17:24:34

标签: javascript html css

我尝试运行我在CodePen上找到的代码,但Chrome和Firefox并不想运行js。我尝试了不同的浏览器但获得了相同的结果。当我在浏览器中检查元素时,我得到了这个:

Sub Mail()
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim OutApp As Object
    Dim OutMail As Object

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set Sourcewb = ActiveWorkbook

    'Copy the ActiveSheet to a new workbook
    ActiveSheet.Copy
    Set Destwb = ActiveWorkbook

    'Determine the Excel version and file extension/format
    With Destwb
        If Val(Application.Version) < 12 Then
            'You use Excel 97-2003
            FileExtStr = ".xls": FileFormatNum = -4143
        Else
            'You use Excel 2007-2016
            Select Case Sourcewb.FileFormat
            Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
            Case 52:
                If .HasVBProject Then
                    FileExtStr = ".xlsm": FileFormatNum = 52
                Else
                    FileExtStr = ".xlsx": FileFormatNum = 51
                End If
            Case 56: FileExtStr = ".xls": FileFormatNum = 56
            Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
            End Select
        End If
    End With

    '    'Change all cells in the worksheet to values if you want
    '    With Destwb.Sheets(1).UsedRange
    '        .Cells.Copy
    '        .Cells.PasteSpecial xlPasteValues
    '        .Cells(1).Select
    '    End With
    '    Application.CutCopyMode = False

    'Save the new workbook/Mail it/Delete it
    TempFilePath = Environ$("temp") & "\"
    TempFileName = "Part of " & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    With Destwb
        .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
        On Error Resume Next
        With OutMail
            .to = "ron@debruin.nl"
            .CC = ""
            .BCC = ""
            .Subject = "This is the Subject line" & Range("A1").Value    '<-- addition
            .Body = "Hi there"
            .Attachments.Add Destwb.FullName
            'You can add other files also like this
            '.Attachments.Add ("C:\test.txt")
            .Send   'or use .Display
        End With
        On Error GoTo 0
        .Close savechanges:=False
    End With

    'Delete the file you have send
    Kill TempFilePath & TempFileName & FileExtStr

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub

我尝试运行的代码可以在这里找到:https://codepen.io/gleamland/pen/KmzrZp

我已将html代码添加到body标签:

script {
display: none;}

我在网站上将js和CSS代码复制/粘贴到名为photo_gallery.css和photo_gallery.js的单独文件中(如上所述)

有谁知道为什么以及如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

您需要添加jQuery,Bootstrap和Magnific Popup。试试这种方式:

<!DOCTYPE html>
<head>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="photo_gallery.css"/>
    <meta charset="UTF-8"/>
</head>
<body>
...
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.1.0/jquery.magnific-popup.min.js"></script>
    <script src="photo_gallery.js"></script>
</body>
</html>

使用<script>中的<body>代码而不是<head>中的代码非常重要,否则无效。

答案 1 :(得分:0)

我建议在head标记之前的<script src="photo_gallery.js"></script></body>添加这两个脚本。

最后添加它的原因是因为HTML可能没有完全加载以使JS工作。因此,在最后添加它可确保加载的HTML和JS可以对其进行操作。

<!DOCTYPE html>
<head>
    <link rel="stylesheet" type="text/css" href="photo_gallery.css"/>
    <meta charset="UTF-8"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.1.0/jquery.magnific-popup.min.js"></script>
    <script src="photo_gallery.js"></script>
</head>
<body>
...
...
...
<script src="photo_gallery.js"></script>
</body>
</html>