Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.21 KB | None | 0 0
  1. var window_background = "white"; // the color of the pop-up window
  2. var window_border = "blue"; // the border color of pop-up window
  3. var text_color = "black"; // the color of the text in window
  4. var title_color = "white"; // color of window title text
  5. var window_width = 200; // width of window
  6. var window_height = 150; // height of window
  7. var mozilla_opt = 1; // change to 0 to use Netscape and Firefox built-in search window
  8. var start_at = 0; // Change to which character you want to start with on the page if IE gives an error because of searching in menus
  9. // Example: start_at = 300, makes the find start at the 300th character on the page
  10. /* Do not edit anything below this line */
  11.  
  12. //var ie = ((navigator.appVersion.indexOf("MSIE")!= -1)&&!window.opera)? true : false; // to detect if IE
  13. var ie = (document.all)
  14. //if (document.getElementById && !document.all)
  15. if (window.find)
  16. var nav = 1; // to detect if netscape or firefox
  17. else
  18. var nav = 0;
  19. var t = 0; // used for timer to move window in IE when scrolling
  20.  
  21. var sel; // Selection object needed for Firefox
  22. var range; // range object needed for Firefox
  23.  
  24. // The following is to capture mouse movement
  25. // If Netscape or Mozilla -- then set up for mouse capture
  26. if (!ie)
  27. {
  28. document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
  29. }
  30.  
  31. document.onmousedown = MouseDown;
  32. document.onmousemove = MouseMove;
  33. document.onmouseup = MouseUp;
  34.  
  35. // Temporary variables to hold mouse x-y pos's
  36. var mousex = 0;
  37. var mousey = 0;
  38.  
  39. if (ie)
  40. {
  41. // this variable will hold all the text on the page for IE
  42. // We are creating a text range from the whole document body
  43. var txt = document.body.createTextRange();
  44.  
  45. // this variable will bookmark the last find position
  46. // in an array of bookmarks so we can keep going to previous finds
  47. var bookmark = new Array();
  48. // bookmark the beginning of the text body
  49. //bookmark[0] = txt.getBookmark();
  50. }
  51.  
  52. // variable to record number fo finds
  53. var finds = 0;
  54.  
  55. function findit()
  56. {
  57. // put the value of the textbox in string
  58. var string = document.getElementById('fwtext').value;
  59.  
  60. // 8-9-2010 Turn DIV to hidden just while searching so doesn't find the text in the window
  61. findwindow.style.visibility = 'hidden';
  62.  
  63. if (ie)
  64. {
  65. // Bookmark this position in bookmark array at finds variable
  66. bookmark[finds] = txt.getBookmark();
  67.  
  68. // findText is IE's javascript function to find text in
  69. // a text range
  70. if (string) // only call findText if there is a string or IE will have error
  71. if (txt.findText(string)) // if found
  72. {
  73. // select() not only highlights the string but
  74. // it also moves the view to that location
  75. txt.select();
  76. // scrollIntoView() is just a duplicate of what
  77. // select does by jumping to the selection, so
  78. // we don't realy need it.
  79. txt.scrollIntoView();
  80. // moveStart('character') moves the position in the
  81. // text one character forward so that we can search
  82. // for the next case of string in the body
  83. //txt.moveStart('character');
  84.  
  85. // collapse moves the insertion point to the end
  86. // of the range so we can search for the next value
  87. txt.collapse(false);
  88.  
  89. test.innerHTML = "Found";
  90. }
  91. else
  92. {
  93. test.innerHTML = "Not found";
  94. finds--;
  95. }
  96. }
  97. else // Netscape or firefox
  98. {
  99. if (finds > 0)
  100. {
  101. sel = window.getSelection(); // get selection
  102. // remove all ranges
  103. if(sel.rangeCount > 0) sel.removeAllRanges();
  104. // add last highlighted range
  105. sel.addRange(range);
  106. }
  107. // window.find(string, caseSensitive, searchBackwards)
  108. //for (i=0;i <= finds; i++)
  109. if (string != "")
  110. test.innerHTML = window.find(string, false, false);
  111. // In Firefox (not Netscape) when we press the
  112. // Next or Prev buttons in the DIV it looses the
  113. // selection, so we have to grab the selection
  114. // locations so we don't keep searching for only
  115. // the first found search over and over
  116. sel = window.getSelection(); // get selection
  117. range = sel.getRangeAt(0); // get object
  118. //test.innerHTML = sel.toString();
  119. }
  120. finds++;
  121. findwindow.style.visibility = 'visible';
  122.  
  123. } // end function findit()
  124.  
  125.  
  126. // This function is to find backwards by pressing the Prev button
  127. function findprev()
  128. {
  129. // put the value of the textbox in string
  130. var string = document.getElementById('fwtext').value;
  131.  
  132. // 8-9-2010 Turn DIV to hidden just while searching so doesn't find the text in the window
  133. findwindow.style.visibility = 'hidden';
  134.  
  135. if (ie)
  136. {
  137. // if they found only 0 or 1 occurance then don't do anything
  138. // because they haven't found enough to go backwards
  139. if (finds < 2)
  140. {
  141. findwindow.style.visibility = 'visible';
  142. return;
  143. }
  144. // Make finds variable go back to previous find
  145. finds = finds - 2; // I don't know why I have to go back 2
  146.  
  147. // move back to previously bookmarked position
  148. txt.moveToBookmark(bookmark[finds]);
  149.  
  150. // select it
  151. findit();
  152. }
  153. else // if netscape or firefox
  154. {
  155. if (finds > 0)
  156. {
  157. sel = window.getSelection(); // get selection
  158. // remove all ranges
  159. if(sel.rangeCount > 0) sel.removeAllRanges();
  160. // add last highlighted range
  161. sel.addRange(range);
  162. }
  163. // window.find(string, caseSensitive, searchBackwards)
  164. if (string != "")
  165. test.innerHTML = window.find(string, false, true);
  166. // In Firefox (not Netscape) when we press the
  167. // Next or Prev buttons in the DIV it looses the
  168. // selection, so we have to grab the selection
  169. // locations so we don't keep searching for only
  170. // the first found search over and over
  171. sel = window.getSelection(); // get selection
  172. range = sel.getRangeAt(0); // get object
  173. }
  174.  
  175. findwindow.style.visibility = 'visible';
  176.  
  177. } // end findprev()
  178.  
  179.  
  180. // This function looks for the ENTER key (13)
  181. // while the find window is open, so that if they user
  182. // press ENTER it will do the find next
  183. function checkkey(e)
  184. {
  185. var keycode;
  186. if (window.event) // if ie
  187. keycode = window.event.keyCode;
  188. else // if Firefox or Netscape
  189. keycode = e.which;
  190.  
  191. //test.innerHTML = keycode;
  192.  
  193. if (keycode == 13) // if ENTER key
  194. {
  195. // For some reason in IE,
  196. // I have to focus on the 'NEXT' button
  197. // or finding by the Enter key does not always work.
  198. if (ie)
  199. document.getElementById('btn').focus();
  200. findit(); // call findit() function (like pressing NEXT)
  201. }
  202. } // end function checkkey()
  203.  
  204.  
  205. // This function makes the findwindow DIV visible
  206. // so they can type in what they want to search for
  207. function show()
  208. {
  209. if (ie || mozilla_opt == 1)
  210. {
  211. //var findwindow = document.getElementById('findwindow');
  212.  
  213. // Object to hold textbox so we can focus on it
  214. // so user can just start typing after "find" button
  215. // is clicked
  216. var textbox = document.getElementById('fwtext');
  217.  
  218. // Make the find window visible
  219. findwindow.style.visibility = 'visible';
  220. //fwtext.style.visibility = 'visible';
  221.  
  222. // Put cursor focus in the text box
  223. textbox.focus();
  224.  
  225. // Call timer to move textbox in case they scroll the window
  226. t = setInterval('move_window();', 500);
  227.  
  228. // Setup to look for keypresses while window is open
  229. document.onkeydown = checkkey;
  230.  
  231. }
  232. else // if netscape or firefox
  233. window.find();
  234. // Note: Netscape and Firefox have a built in find window
  235. // that can be called with window.find()
  236. // They also have a find like IE's with self.find(string, 0, 1)
  237. // But I can't find any instructions on how to use it to
  238. // keep searching forward on "Next" button presses
  239.  
  240. } // end function show()
  241.  
  242.  
  243. // This function makes the findwindow DIV hidden
  244. // for when they click on close
  245. function hide()
  246. {
  247. //var findwindow = document.getElementById('findwindow');
  248.  
  249. findwindow.style.visibility = 'hidden';
  250.  
  251. // turn off timer to move window on scrolling
  252. clearTimeout(t);
  253.  
  254. // Make document no longer look for enter key
  255. document.onkeydown = null;
  256.  
  257. } // end function hide()
  258.  
  259.  
  260. // This function resets the txt selection pointer to the
  261. // beginning of the body so that we can search from the
  262. // beginning for the new search string when somebody
  263. // enters new text in the find box
  264. function resettext()
  265. {
  266. if (ie)
  267. {
  268. txt = document.body.createTextRange();
  269. txt.moveStart("character", start_at);
  270. //txt.select();
  271. }
  272. finds = 0;
  273. } // end function reset()
  274.  
  275.  
  276. // This function makes the find window jump back into view
  277. // if they scroll while it is open or if the page automatically
  278. // scrolls when it is hightlighting the next found text
  279. function move_window()
  280. {
  281. //var findwindow = document.getElementById('findwindow');
  282.  
  283. // get current left, top and height of find_window
  284. fwtop = parseFloat(findwindow.style.top);
  285. fwleft = parseFloat(findwindow.style.left);
  286. fwheight = parseFloat(findwindow.style.height);
  287.  
  288. // get current top and bottom position of browser screen
  289. if (document.documentElement.scrollTop) // Needed if you use doctype loose.htm
  290. current_top = document.documentElement.scrollTop;
  291. else
  292. current_top = document.body.scrollTop;
  293. if (document.documentElement.clientHeight)
  294. {
  295. if (document.documentElement.clientHeight > document.body.clientHeight)
  296. current_bottom = document.body.clientHeight + current_top;
  297. else
  298. current_bottom = document.documentElement.clientHeight + current_top;
  299. }
  300. else
  301. current_bottom = document.body.clientHeight + current_top;
  302.  
  303. // get current left and right position of browser
  304. if (document.documentElement.scrollLeft) // Needed if you use doctype loose.htm
  305. current_left = document.documentElement.scrollLeft;
  306. else
  307. current_left = document.body.scrollLeft;
  308. if (document.documentElement.clientWidth)
  309. {
  310. if (document.documentElement.clientWidth > document.body.clientWidth)
  311. current_right = document.body.clientWidth + current_left;
  312. else
  313. current_right = document.documentElement.clientWidth + current_left;
  314. }
  315. else
  316. current_right = document.body.clientWidth + current_left;
  317.  
  318. //test.innerHTML = current_right + ',' + current_left;
  319.  
  320. // Only move window if it is out of the view
  321. if (fwtop < current_top)
  322. {
  323. // move window to current_top
  324. findwindow.style.top = current_top + 'px';
  325. }
  326. else if (fwtop > current_bottom - fwheight)
  327. {
  328. // move to current_bottom
  329. findwindow.style.top = current_bottom - fwheight + 'px';
  330. }
  331.  
  332. // Only move left position if out of view
  333. if (fwleft < current_left ||
  334. fwleft > current_right)
  335. {
  336. findwindow.style.left = current_left + 'px';
  337. }
  338.  
  339. /* var test = document.getElementById('test');
  340. test.innerHTML = 'find window: ' + fwtop
  341. + ' curr_bottom: ' + current_bottom; */
  342.  
  343. } // end function move_window()
  344.  
  345.  
  346. function MouseDown(e)
  347. {
  348. if (over == 1)
  349. DivID = 'findwindow';
  350.  
  351. if (over)
  352. {
  353. if (ie)
  354. {
  355. objDiv = document.getElementById(DivID);
  356. objDiv = objDiv.style;
  357. mousex=event.offsetX;
  358. mousey=event.offsetY;
  359. }
  360. else // if Mozilla or Netscape
  361. {
  362. objDiv = document.getElementById(DivID);
  363. mousex=e.layerX;
  364. mousey=e.layerY;
  365. return false;
  366. }
  367. }
  368. }
  369.  
  370.  
  371.  
  372. function MouseMove(e)
  373. {
  374.  
  375. // get current top
  376. if (document.documentElement.scrollTop) // Needed if you use doctype loose.htm
  377. current_top = document.documentElement.scrollTop;
  378. else
  379. current_top = document.body.scrollTop;
  380.  
  381.  
  382. // get current left
  383. if (document.documentElement.scrollLeft) // Needed if you use doctype loose.htm
  384. current_top = document.documentElement.scrollLeft;
  385. else
  386. current_left = document.body.scrollLeft;
  387.  
  388.  
  389. if (objDiv)
  390. {
  391. if (ie)
  392. {
  393. objDiv.pixelLeft = event.clientX-mousex + current_left;
  394. objDiv.pixelTop = event.clientY-mousey + current_top;
  395. return false;
  396. }
  397. else // if Mozilla or Netscape
  398. {
  399. objDiv.style.left = (e.pageX-mousex) + 'px';
  400. objDiv.style.top = (e.pageY-mousey) + 'px';
  401. return false;
  402. }
  403. }
  404. } // end function MouseMove(e)
  405.  
  406. //
  407. //
  408. //
  409. function MouseUp()
  410. {
  411. objDiv = null;
  412. }
  413.  
  414.  
  415.  
  416. // Create findwindow DIV but make it invisible
  417. // It will be turned visible when user clicks on
  418. // the "Find on this page..." button
  419.  
  420. document.write('<div id="findwindow" style="position:absolute'
  421. + ';left: 0px; top: 0px'
  422. + ';visibility: hidden'
  423. + ';background-color: ' + window_background
  424. + ';border: 2px solid ' + window_border
  425. + ';width: ' + window_width + 'px'
  426. + ';height: ' + window_height + 'px'
  427. + ';color: ' + text_color
  428. + ';padding: 0px'
  429. + ';font-size: 14px'
  430. + ';"'
  431. + '>');
  432. // This part creates the titlebar
  433. document.write('<div style="text-align: center'
  434. + ';width: ' + (window_width-20) + 'px'
  435. + ';cursor: move' // turn mouse arrow to move icon
  436. + ';color: ' + title_color
  437. + ';border: 1px solid ' + text_color
  438. + ';background-color: ' + window_border
  439. + ';float: left'
  440. + ';" onmouseover="over=1;" onmouseout="over=0;">'
  441. + 'Find Window</div>');
  442. // This part creates the closing X
  443. document.write('<div onclick="hide();" style="text-align: center'
  444. + ';width: ' + (16) + 'px'
  445. + ';cursor: default' // make mouse arrow stay an arrow instead of turning to text arrow
  446. + ';font-weight: bold'
  447. + ';background-color: red'
  448. + ';border: 1px solid ' + text_color
  449. + ';float: right'
  450. + ';">'
  451. + 'X' // write the letter X
  452. + '</div><br />\n');
  453. // This part creates the instructions and the "find" button
  454. document.write('<div id="window_body" style="padding: 5px;">'
  455. + 'Type in text to find: '
  456. + '<input type="text" size="25" maxlength="25" id="fwtext"'
  457. + ' onchange="resettext();">'
  458. + '<input type="button" value="Find Prev" onclick="findprev();">'
  459. + '<input id="btn" type="button" value="Find Next" onclick="findit();">'
  460. + '</div>\n'
  461. + '<div id="test"><br /></div>');
  462. document.write('</div>');
  463.  
  464.  
  465. // This part creates a visible button on the HTML page to
  466. // where the script is pasted in the HTML code
  467. document.write('<input type="button" value="Find Item"'
  468. + ' onclick="show();">');
  469.  
  470. // hold the findwindow DIV in findwindow
  471. var findwindow = document.getElementById('findwindow');
  472.  
  473. // over variable is whether mouse pointer is over the DIV to move
  474. var over = 0;
  475.  
  476. // Object to hold findwindow for MouseMove
  477. var objDiv = null;
  478.  
  479. // ID of DIV for MouseMove functions
  480. var DivID = null;
  481.  
  482. var test = document.getElementById('test');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement