Skip to content

Commit adaca89

Browse files
committed
fix scripts
1 parent 03f7460 commit adaca89

2 files changed

Lines changed: 33 additions & 20 deletions

File tree

demo.html

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
<meta charset="utf-8"/>
66
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
77
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
8-
<script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.min.js"></script>
9-
<script type="text/javascript" src="lib/String_random.js"></script>
108

119
<meta name="keywords" content="JavaScript,Library,RegExp,Regular Expression,Random String"/>
1210
<meta name="description" content="String_random.js is a library for generating random string from a regular experession."/>
@@ -40,10 +38,10 @@ <h1>Demo of String_random.js</h1>
4038
<input type="text" name="regexp" placeholder="RegExp" class="form-control input-lg" style="" required/>
4139
</div>
4240
<input type="submit" value="Generate" class="btn btn-primary btn-lg"/>
43-
<div>
41+
<div id="examples">
4442
Examples:
45-
<a href="javascript:generate(/\d+/)">Numbers</a>,
46-
<a href="javascript:generate(/https?:\/\/[a-z]{3,8}\.example\.com\/([a-z\d]+\/){3}/)">URL</a>,
43+
<a href="javascript:void()" data-regexp="\d+">Numbers</a>,
44+
<a href="javascript:void()" data-regexp="https?:\/\/[a-z]{3,8}\.example\.com\/([a-z\d]+\/){3}">URL</a>,
4745
<!--
4846
<a href='javascript:generate(/(?:&lt;(p|div) title="[^"]+"&gt;[^&lt;]+&lt;\/\1&gt;)+/)'>HTML</a>,
4947
-->
@@ -58,7 +56,9 @@ <h1>Demo of String_random.js</h1>
5856
</div>
5957
</div>
6058

61-
<script>
59+
<script type="module">
60+
import { String_random } from './lib/String_random.js';
61+
6262
(function(window) {
6363

6464
// exit if the browser implements that event
@@ -91,39 +91,51 @@ <h1>Demo of String_random.js</h1>
9191

9292

9393
function generate (re) {
94-
var input = $('input[name=regexp]');
95-
var output = $('#output').empty();
96-
94+
var input = document.querySelector('input[name=regexp]');
95+
var output = document.getElementById('output');
96+
output.innerHTML = '';
9797

9898
if (!re) {
99-
re = input.val();
99+
re = input.value;
100100
if (!re) return;
101101
}
102102

103-
input.val(re.source || re);
104-
location.hash = '#' + encodeURIComponent(input.val());
105-
document.title = input.val();
103+
input.value = re.source || re;
104+
location.hash = '#' + encodeURIComponent(input.value);
105+
document.title = input.value;
106106

107107
for (var i = 0; i < 10; i++) {
108108
var generated = String_random(re);
109109

110-
var p = $('<p/>').text(generated);
110+
var p = document.createElement('p');
111+
p.textContent = generated;
111112
const params = new URLSearchParams();
112113
params.set('url', location.href);
113114
params.set('text', `${generated} #string_random_js`);
114115

115116
var url = `https://twitter.com/intent/tweet?${params.toString()}`;
116117

117-
$('<a href="" class="btn btn-sm btn-info twitter-share-button">Tweet</a>').
118-
attr('href', url).
119-
appendTo(p);
120-
p.appendTo(output);
118+
var a = document.createElement('a');
119+
a.href = url;
120+
a.className = 'btn btn-sm btn-info twitter-share-button';
121+
a.textContent = 'Tweet';
122+
p.appendChild(a);
123+
output.appendChild(p);
121124
}
122125
}
123126

124-
$("form").submit(function () {
127+
document.getElementById('examples').addEventListener('click', function (e) {
128+
if (e.target.tagName === 'A') {
129+
e.preventDefault();
130+
e.stopPropagation();
131+
const regexp = e.target.getAttribute('data-regexp');
132+
generate(regexp);
133+
}
134+
});
135+
136+
document.querySelector('form').addEventListener('submit', function (e) {
125137
generate();
126-
return false;
138+
e.preventDefault();
127139
});
128140

129141
window.onhashchange = function (a, b) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"test": "test"
99
},
1010
"scripts": {
11+
"serve": "npx serve .",
1112
"test": "node --test test/test.js"
1213
},
1314
"repository": "",

0 commit comments

Comments
 (0)