-1

I am trying to include the library https://github.com/jakubroztocil/rrule to my webpage.

But I recieve the error: Uncaught SyntaxError: Unexpected token {

I have tried with

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script src="https://jakubroztocil.github.io/rrule/dist/es5/rrule-tz.min.js"></script>

<script>
$(document).ready(function(){

import { RRule, RRuleSet, rrulestr } from 'rrule'
alert(rrulestr('DTSTART:20120201T023000Z\nRRULE:FREQ=MONTHLY;COUNT=5')
)

});
</script>
</head>
<body>



</body>
</html>
Shivam Pandey
  • 3,756
  • 2
  • 19
  • 26
Code Guy
  • 3,059
  • 2
  • 30
  • 74
  • 3
    I think `import {}` syntax is not supported natively in browsers. Put it in a file and use a transpiler like babel to get js file, and use that. Or use normal JS syntax. –  Mar 16 '19 at 07:41
  • I tried with import * from 'rrule' but I am not successful. – Code Guy Mar 16 '19 at 15:06
  • 1
    @psinaught `import` is supported if the file was loaded as a module. – Evert Mar 19 '19 at 04:13

2 Answers2

2

Working code

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script src="https://jakubroztocil.github.io/rrule/dist/es5/rrule-tz.min.js"></script>

<script>
$(document).ready(function(){

alert(rrule.rrulestr('DTSTART:20120201T023000Z\nRRULE:FREQ=MONTHLY;COUNT=5'));

});
</script>
</head>
<body>



</body>
</html>
  • @Code Guy As far as I understand - `{` in `import {` –  Mar 21 '19 at 12:35
  • It would be helpful if you eloborated on your solution for future readers. What was the error, how does your answer solve that? – viam0Zah Jun 29 '20 at 11:56
0

Import does not work inside script tag, use below solution where a rrule minified js is inserted and functions used.

<!DOCTYPE html>
<html>

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <script src="https://jakubroztocil.github.io/rrule/dist/es5/rrule-tz.min.js"></script>

    <script>
        $(document).ready(function () {
            const RRule = rrule.RRule;

            const rule = new RRule({
                freq: RRule.WEEKLY,
                interval: 5,
                byweekday: [RRule.MO, RRule.FR],
                dtstart: new Date(Date.UTC(2015, 1, 1, 10, 30)),
                until: new Date(Date.UTC(2020, 12, 31))
            });

            alert(rule.between(new Date(Date.UTC(2018, 7, 1)), new Date(Date.UTC(2019, 8, 1))));
        });
    </script>
</head>

<body>

</body>

</html>
Shivam Pandey
  • 3,756
  • 2
  • 19
  • 26