/    Sign up×
Community /Pin to ProfileBookmark

Inserting random breaks into a text/htmp document

Can anyone help me design a script that takes a paragraph of text such as

<p>It’s cool technology, but what about the humans? The debate still rages years after Semantic Web’s introduction.<P>

and inserts <fdfr> (random sets of 2 , 3 and 4 letters) in-between the words so it looks like:

<p>It’s c<gfr>ool tec<fd>hnol<gfgt>ogy, b<fdr>ut wh<llf>at ab<frd>ou<fdfr>t th<fgdf>e h<fdf>umans? T<gft>he d<vfdf>eba<cdfd>te s<df>till r<dfff>age<fdf>s y<df>ea<fgt>rs af<fdfr>ter S<fdf>emantic Web’s introduction.<P>

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERMay 27.2008 — Try this or modify to your needs: ?
[code=php]
<html>
<head>
<title>Random Insert</title>
<script type="text/javascript">
function InsText() {
var alpha = 'abcdedfghijklmnopqrstuvwxyz';
var s = '&lt;';
var r = Math.floor(Math.random()*2)+2;
var p = 0;
for (j=0; j<r; j++) {
p = Math.floor(Math.random()*26);
s += alpha.charAt(p);
}
s += '&gt;';
return s;
}
function RandomInsert() {
var p, t;
var txt = document.getElementById('orig').innerHTML;
var txtarr = txt.split('');
var str = '';
for (i=0; i<txtarr.length; i++) {
str += txtarr[i];
if (Math.random() < 0.25) { str += InsText(); }
}
// alert(str);
document.getElementById('rslt').innerHTML = str;
}
</script>
</head>
<body>
<p>
<div id='orig'>
It's cool technology, but what about the humans?
The debate still rages years after Semantic Web's introduction.
</div>
<p>
<button onClick="RandomInsert()">Random Insert</button>
<div id="rslt"></div>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@hhedayaauthorMay 27.2008 — I need to pick up the result with the coded words, this just seems to return the orig. sentence words.

the result should look like:

Th<fgrg>e Qui<fdf>ck Br<dfd>own F<gh>ox etc...Im i missing something?
Copy linkTweet thisAlerts:
@JMRKERMay 27.2008 — I guess I'm missing something.

The result can be returned as the 'str' value at the end of

the 'RandomInsert()' function instead of posting to the <DIV> area.

Try putting an alert(str) at the end of the function to see the results to be returned.

How else do you want? Is it not working for you? Are you getting any errors?
Copy linkTweet thisAlerts:
@hhedayaauthorMay 27.2008 — I would like the returned text to show the <random letters> so I can copy and paste it into a notepad file...
Copy linkTweet thisAlerts:
@JMRKERMay 27.2008 — Instead of:
[code=php]
<div id="rslt"></div>
[/code]


Try:
[code=php]
<textarea id="rslt"></textarea>
[/code]


?
Copy linkTweet thisAlerts:
@hhedayaauthorMay 27.2008 — this produces a script error:

<html>

<head>

<title>Random Insert</title>

<script type="text/javascript">

function InsText() {

var alpha = 'abcdedfghijklmnopqrstuvwxyz';

var s = '<';

var r = Math.floor(Math.random()*2)+2;

var p = 0;

for (j=0; j<r; j++) {

p = Math.floor(Math.random()*
26);

s += alpha.charAt(p);

}

s += '>';

return s;

}

function RandomInsert() {

var p, t;

var txt = document.getElementById('orig').innerHTML;

var txtarr = txt.split('');

var str = '';

for (i=0; i<txtarr.length; i++) {

str += txtarr[i];

if (Math.random() < 0.25) { str += InsText(); }

}

// alert(str);

document.getElementById('rslt').innerHTML = str; } </script> </head> <body> <p> <div id='orig'> It's cool technology, but what about the humans?

The debate still rages years after Semantic Web's introduction.

</div>

<p>

<button onClick="RandomInsert()">Random Insert</button> <textarea id="rslt"></textarea> </body> </html>
Copy linkTweet thisAlerts:
@hhedayaauthorMay 27.2008 — is there any way I can import source text from a .txt file ( a browse button) this is awsome!
Copy linkTweet thisAlerts:
@JMRKERMay 27.2008 — Consider using an 'ajax' method to import your text.

Forum has a bunch of information and references for tutorials.
Copy linkTweet thisAlerts:
@hhedayaauthorMay 27.2008 — How do I combine this AJAX script with my random text generator script:

AJAX

<script type="text/javascript">

var http = false;

if(navigator.appName == "Microsoft Internet Explorer") {

http = new ActiveXObject("Microsoft.XMLHTTP");

} else {

http = new XMLHttpRequest();

}

function replace() {

http.open("GET", "test.txt", true);

http.onreadystatechange=function() {

if(http.readyState == 4) {

document.getElementById('foo').innerHTML = http.responseText;

}

}

http.send(null);

}

</script>

<p><a href="javascript:replace()">Replace Text</a></p>

<div id="foo">

Hello, world!

</div>

Random Text:

<html>

<head>

<title>Random Insert</title>

<script type="text/javascript">

function InsText() {

var alpha = 'abcdedfghijklmnopqrstuvwxyz';

var s = '<';

var r = Math.floor(Math.random()*2)+2;

var p = 0;

for (j=0; j<r; j++) {

p = Math.floor(Math.random()*
26);

s += alpha.charAt(p);

}

s += '>';

return s;

}

function RandomInsert() {

var p, t;

var txt = document.getElementById('orig').innerHTML;

var txtarr = txt.split('');

var str = '';

for (i=0; i<txtarr.length; i++) {

str += txtarr[i];

if (Math.random() < 0.25) { str += InsText(); }

}

// alert(str);

document.getElementById('rslt').innerHTML = str; } </script> </head> <body> <p> <div id='orig'> If you can type and pay close attention to details, then we may have a position for you.

We are currently hiring assistants to work in a very fast paced and exciting environment.



Ideal candidates must be be at least 18 years of age with good communication skills.

As an assitant your job duties will include work in various departments, and may include the following:



Filing

Typing Memos

Mailing

Preparing Presentations

Research

Answering Phones

Errands

Computer Work (MS Word, Excel, and Internet)

Problem Solving

General Office Work

and more.



If you feel you can perform these tasks, start here:</div>

<p>

<button onClick="RandomInsert()">Random Insert</button> <textarea id="rslt" rows="1" cols="20"></textarea> </body> </html>
×

Success!

Help @hhedaya spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...